2016-04-03 01:10:40 +00:00
|
|
|
/* threadless.io
|
|
|
|
* Copyright (c) 2016 Justin R. Cutler
|
|
|
|
* Licensed under the MIT License. See LICENSE file in the project root for
|
|
|
|
* full license information.
|
|
|
|
*/
|
|
|
|
/** @file
|
|
|
|
* default allocator implementation
|
|
|
|
* @author Justin R. Cutler <justin.r.cutler@gmail.com>
|
|
|
|
*/
|
2016-04-04 00:52:32 +00:00
|
|
|
|
2016-04-03 01:10:40 +00:00
|
|
|
/* realloc */
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/* ... */
|
2016-04-04 00:52:32 +00:00
|
|
|
#include <threadless/default_allocator.h>
|
2016-04-03 01:10:40 +00:00
|
|
|
|
|
|
|
|
2016-04-04 00:52:32 +00:00
|
|
|
static int default_allocate(allocation_t *allocation, size_t size)
|
|
|
|
{
|
|
|
|
void *new_memory;
|
2016-04-03 01:10:40 +00:00
|
|
|
|
2016-04-04 00:52:32 +00:00
|
|
|
/* perform allocator action */
|
|
|
|
new_memory = realloc(allocation->memory, size);
|
2016-04-03 01:10:40 +00:00
|
|
|
|
2016-04-04 00:52:32 +00:00
|
|
|
if ((NULL == new_memory) && (size != 0)) {
|
|
|
|
/* realloc failed */
|
|
|
|
return -1;
|
2016-04-03 01:10:40 +00:00
|
|
|
}
|
2016-04-04 00:52:32 +00:00
|
|
|
|
|
|
|
/* update allocation */
|
|
|
|
allocation->memory = new_memory;
|
|
|
|
allocation->size = size;
|
|
|
|
|
|
|
|
return 0;
|
2016-04-03 01:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-09 03:06:41 +00:00
|
|
|
static void default_destroy(allocator_t *allocator)
|
2016-04-03 01:10:40 +00:00
|
|
|
{
|
|
|
|
/* ignore allocator */
|
|
|
|
(void) allocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static allocator_t default_allocator = {
|
|
|
|
.allocate = default_allocate,
|
2016-04-09 03:06:41 +00:00
|
|
|
.destroy = default_destroy,
|
2016-04-03 01:10:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-04-04 15:09:27 +00:00
|
|
|
allocator_t *default_allocator_get(void)
|
2016-04-03 01:10:40 +00:00
|
|
|
{
|
|
|
|
return &default_allocator;
|
|
|
|
}
|