diff options
| author | tanaka takashi <isazaka@gmail.com> | 2026-06-29 21:43:54 +0900 |
|---|---|---|
| committer | tanaka takashi <isazaka@gmail.com> | 2026-06-30 01:23:51 +0900 |
| commit | 4bc721c3bf7589b519c5730d093f704ce56e3f09 (patch) | |
| tree | 38da055ab15fd70f5c92584560ea73486e5ddce0 /src/base | |
| parent | 4c6a831aa2a57e2bcc20aa3fc2f1b7172f5be285 (diff) | |
refactor: MCP層をmcpc APIベースに刷新
- 独自JSON-RPC処理をmcpcへ置き換え
- 各MCPツールを個別ソースへ分離
- 共通コンテキストと登録処理を整理
- mcpc互換修正と統合テストを更新
Diffstat (limited to 'src/base')
| -rw-r--r-- | src/base/allocator.c | 107 | ||||
| -rw-r--r-- | src/base/allocator.h | 23 |
2 files changed, 130 insertions, 0 deletions
diff --git a/src/base/allocator.c b/src/base/allocator.c new file mode 100644 index 0000000..3a5c515 --- /dev/null +++ b/src/base/allocator.c @@ -0,0 +1,107 @@ +#define TINY_ALLOCATOR_IMPLEMENTATION +#include "base/allocator.h" + +#include <stdint.h> +#include <stdlib.h> + +static size_t fail_index = SIZE_MAX; +static size_t calls; +static size_t outstanding; + +static int +should_fail(void) +{ + return calls++ == fail_index; +} + +void * +tiny_alloc(size_t size) +{ + void *result; + if (should_fail()) + { + return NULL; + } + result = malloc(size); + if (result != NULL) + { + outstanding++; + } + return result; +} + +void * +tiny_calloc(size_t count, size_t size) +{ + void *result; + if ((size != 0 && count > SIZE_MAX / size) || should_fail()) + { + return NULL; + } + result = calloc(count, size); + if (result != NULL) + { + outstanding++; + } + return result; +} + +void * +tiny_realloc(void *memory, size_t size) +{ + void *result; + if (size == 0) + { + tiny_free(memory); + return NULL; + } + if (should_fail()) + { + return NULL; + } + result = realloc(memory, size); + if (result != NULL && memory == NULL) + { + outstanding++; + } + return result; +} + +void +tiny_free(void *memory) +{ + if (memory != NULL) + { + free(memory); + if (outstanding > 0) + { + outstanding--; + } + } +} + +void +tiny_allocator_reset(void) +{ + fail_index = SIZE_MAX; + calls = 0; +} + +void +tiny_allocator_fail_after(size_t allocation_index) +{ + fail_index = allocation_index; + calls = 0; +} + +size_t +tiny_allocator_call_count(void) +{ + return calls; +} + +size_t +tiny_allocator_outstanding(void) +{ + return outstanding; +} diff --git a/src/base/allocator.h b/src/base/allocator.h new file mode 100644 index 0000000..626e294 --- /dev/null +++ b/src/base/allocator.h @@ -0,0 +1,23 @@ +#ifndef TINY_CONVMEM_BASE_ALLOCATOR_H +#define TINY_CONVMEM_BASE_ALLOCATOR_H + +#include <stddef.h> + +void *tiny_alloc(size_t size); +void *tiny_calloc(size_t count, size_t size); +void *tiny_realloc(void *memory, size_t size); +void tiny_free(void *memory); + +void tiny_allocator_reset(void); +void tiny_allocator_fail_after(size_t allocation_index); +size_t tiny_allocator_call_count(void); +size_t tiny_allocator_outstanding(void); + +#ifndef TINY_ALLOCATOR_IMPLEMENTATION +#define malloc tiny_alloc +#define calloc tiny_calloc +#define realloc tiny_realloc +#define free tiny_free +#endif + +#endif |
