mirror of
https://github.com/jrcutler/threadless.io.git
synced 2024-07-07 10:35:49 +00:00
Rewrote allocator/allocation interface to track individual allocations and sizes
This commit is contained in:
42
src/allocation.c
Normal file
42
src/allocation.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/* 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
|
||||
* allocation interface implementation
|
||||
* @author Justin R. Cutler <justin.r.cutler@gmail.com>
|
||||
*/
|
||||
|
||||
/* errno, ENOMEM */
|
||||
#include <errno.h>
|
||||
/* size_t, NULL */
|
||||
#include <stddef.h>
|
||||
|
||||
/* ... */
|
||||
#include <threadless/allocation.h>
|
||||
|
||||
|
||||
#ifndef SIZE_MAX
|
||||
# define SIZE_MAX ((size_t)-1)
|
||||
#endif
|
||||
|
||||
#define SQRT_SIZE_MAX_PLUS_1 ((size_t)1 << (sizeof(size_t) * 4))
|
||||
|
||||
|
||||
int allocation_realloc_array(allocation_t *allocation, size_t nmemb,
|
||||
size_t size)
|
||||
{
|
||||
/* test for multiplication overflow */
|
||||
if (((nmemb >= SQRT_SIZE_MAX_PLUS_1) || (size >= SQRT_SIZE_MAX_PLUS_1)) &&
|
||||
(nmemb != 0) && ((SIZE_MAX / nmemb) < size)) {
|
||||
/* overflow detected */
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t alloc_size = nmemb * size;
|
||||
|
||||
/* perform allocator action */
|
||||
return allocation->allocator->allocate(allocation, alloc_size);
|
||||
}
|
@@ -19,6 +19,8 @@
|
||||
/* ucontext_t, getcontext, makecontext, swapcontext */
|
||||
#include <ucontext.h>
|
||||
|
||||
/* allocator_t allocation_t, allocation_init, allocation_realloc_array */
|
||||
#include <threadless/allocation.h>
|
||||
/* ... */
|
||||
#include <threadless/coroutine.h>
|
||||
|
||||
@@ -29,11 +31,11 @@ enum {
|
||||
|
||||
|
||||
struct coroutine {
|
||||
allocator_t *allocator;
|
||||
ucontext_t context;
|
||||
ucontext_t caller;
|
||||
void *data;
|
||||
int status;
|
||||
allocation_t allocation;
|
||||
ucontext_t context;
|
||||
ucontext_t caller;
|
||||
void *data;
|
||||
int status;
|
||||
};
|
||||
|
||||
|
||||
@@ -67,13 +69,15 @@ coroutine_t *coroutine_create(allocator_t *allocator,
|
||||
goto fail;
|
||||
}
|
||||
|
||||
coro = allocator_malloc(allocator, alloc_size);
|
||||
if (NULL == coro) {
|
||||
allocation_t allocation;
|
||||
allocation_init(&allocation, allocator);
|
||||
if (allocation_realloc_array(&allocation, 1, alloc_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
coro = allocation.memory;
|
||||
memset(coro, 0, alloc_size);
|
||||
coro->allocator = allocator;
|
||||
coro->allocation = allocation;
|
||||
(void) getcontext(&coro->context);
|
||||
coro->context.uc_stack.ss_sp = coro + 1;
|
||||
coro->context.uc_stack.ss_size = stack_size;
|
||||
@@ -92,8 +96,9 @@ fail:
|
||||
|
||||
void coroutine_destroy(coroutine_t *coro)
|
||||
{
|
||||
if (NULL != coro && NULL != coro->allocator) {
|
||||
allocator_free(coro->allocator, coro);
|
||||
if (NULL != coro) {
|
||||
allocation_t allocation = coro->allocation;
|
||||
allocation_free(&allocation);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -7,36 +7,31 @@
|
||||
* default allocator implementation
|
||||
* @author Justin R. Cutler <justin.r.cutler@gmail.com>
|
||||
*/
|
||||
/* errno, ENOMEM */
|
||||
#include <errno.h>
|
||||
|
||||
/* realloc */
|
||||
#include <stdlib.h>
|
||||
|
||||
/* ... */
|
||||
#include <threadless/allocator.h>
|
||||
#include <threadless/default_allocator.h>
|
||||
|
||||
|
||||
#ifndef SIZE_MAX
|
||||
# define SIZE_MAX ((size_t)-1)
|
||||
#endif
|
||||
|
||||
#define SQRT_SIZE_MAX_PLUS_1 ((size_t)1 << (sizeof(size_t) * 4))
|
||||
|
||||
|
||||
static void *default_allocate(allocator_t *allocator, void *ptr, size_t nmemb,
|
||||
size_t size)
|
||||
static int default_allocate(allocation_t *allocation, size_t size)
|
||||
{
|
||||
/* ignore allocator */
|
||||
(void) allocator;
|
||||
/* test for multiplication overflow */
|
||||
if (((nmemb >= SQRT_SIZE_MAX_PLUS_1) || (size >= SQRT_SIZE_MAX_PLUS_1)) &&
|
||||
(nmemb != 0) && ((SIZE_MAX / nmemb) < size)) {
|
||||
/* overflow detected */
|
||||
errno = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
void *new_memory;
|
||||
|
||||
/* perform allocator action */
|
||||
return realloc(ptr, size * nmemb);
|
||||
new_memory = realloc(allocation->memory, size);
|
||||
|
||||
if ((NULL == new_memory) && (size != 0)) {
|
||||
/* realloc failed */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* update allocation */
|
||||
allocation->memory = new_memory;
|
||||
allocation->size = size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user