{
char stack[ALLOCATOR_SIZE];
char *heap;
+ size_t heap_size;
} Allocator;
static inline void
_allocator_init(Allocator* allocator)
{
- if (allocator) allocator->heap = NULL;
+ if (allocator)
+ {
+ allocator->heap = NULL;
+ allocator->heap_size = 0;
+ }
}
static inline void
{
free(allocator->heap);
allocator->heap = NULL;
+ allocator->heap_size = 0;
}
}
return allocator->stack;
}
//fallback to heap
- if (allocator->heap) free(allocator->heap);
+ if (allocator->heap && allocator->heap_size < (size + 1))
+ {
+ free(allocator->heap);
+ allocator->heap = NULL;
+ allocator->heap_size = 0;
+ }
- allocator->heap = malloc(size + 1);
+ if (!allocator->heap)
+ {
+ allocator->heap = malloc(size + 1);
+ allocator->heap_size = (size + 1);
+ }
- if (!allocator->heap) return NULL;
+ if (!allocator->heap)
+ {
+ allocator->heap_size = 0;
+ return NULL;
+ }
memcpy(allocator->heap, str, size);
allocator->heap[size] = '\0';
{
/* immediate - not handled here */
}
- _allocator_reset(&allocator);
}
+
+ _allocator_reset(&allocator);
}
/**