mirror of
https://github.com/jrcutler/threadless.io.git
synced 2024-07-07 10:35:49 +00:00
Switch to a realloc_array()-style allocator (with default implementation)
This commit is contained in:
96
threadless/allocator.h
Normal file
96
threadless/allocator.h
Normal file
@ -0,0 +1,96 @@
|
||||
/* 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
|
||||
* allocator interface definition
|
||||
* @author Justin R. Cutler <justin.r.cutler@gmail.com>
|
||||
*/
|
||||
#ifndef THREADLESS_ALLOCATOR_H
|
||||
#define THREADLESS_ALLOCATOR_H
|
||||
|
||||
/* size_t, NULL */
|
||||
#include <stddef.h>
|
||||
|
||||
/** Memory allocator instance */
|
||||
typedef struct allocator allocator_t;
|
||||
|
||||
/** Memory allocator function
|
||||
* @param[in,out] allocator allocator instance
|
||||
* @param[in,out] ptr memory to reallocate/free (or @c NULL to allocate)
|
||||
* @param nmemb number of elements to allocate (or 0 to free)
|
||||
* @param size size of each element to allocate (or 0 to free)
|
||||
* @retval non-NULL successful allocation/reallocation
|
||||
* @retval NULL successful free (@p nmemb or @p size was 0) or error
|
||||
* @note Upon detection of integer overflow, this function shall return
|
||||
* @c NULL (like @c calloc(3) and FreeBSD's @c realloc_array(3))
|
||||
*/
|
||||
typedef void *(allocator_function_t)(allocator_t *allocator, void *ptr,
|
||||
size_t nmemb, size_t size);
|
||||
|
||||
/** Memory allocator destructor function
|
||||
* @param[in,out] allocator allocator instance
|
||||
* @post @p allocator may no longer be used
|
||||
*/
|
||||
typedef void (allocator_destroy_function_t)(allocator_t *allocator);
|
||||
|
||||
/** Memory allocator instance structure */
|
||||
struct allocator {
|
||||
/** Memory allocator function */
|
||||
allocator_function_t *const allocate;
|
||||
/** Memory allocator instance destructor */
|
||||
allocator_destroy_function_t *const destroy;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/** Get default allocator instance
|
||||
* @returns default allocator
|
||||
*/
|
||||
allocator_t *allocator_get_default(void);
|
||||
|
||||
/** @c malloc() helper function
|
||||
* @param[in,out] allocator allocator
|
||||
* @param size size to allocate
|
||||
* @retval Non-NULL allocated memory
|
||||
* @retval NULL error
|
||||
*/
|
||||
static inline void *allocator_malloc(allocator_t *allocator, size_t size)
|
||||
{
|
||||
return allocator->allocate(allocator, NULL, 1, size);
|
||||
}
|
||||
|
||||
/** @c realloc_array() helper function
|
||||
* @copydetails allocator_function_t
|
||||
*/
|
||||
static inline void *allocator_realloc_array(allocator_t *allocator, void *ptr,
|
||||
size_t nmemb, size_t size)
|
||||
{
|
||||
return allocator->allocate(allocator, ptr, nmemb, size);
|
||||
}
|
||||
|
||||
/** @c free() helper function
|
||||
* @param[in,out] allocator allocator
|
||||
* @param ptr memory to free
|
||||
*/
|
||||
static inline void allocator_free(allocator_t *allocator, void *ptr)
|
||||
{
|
||||
(void) allocator->allocate(allocator, ptr, 0, 0);
|
||||
}
|
||||
|
||||
/** Memory allocator destructor helper function
|
||||
* @copydetails allocator_destroy_function_t
|
||||
*/
|
||||
static inline void allocator_destroy(allocator_t *allocator)
|
||||
{
|
||||
allocator->destroy(allocator);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* THREADLESS_ALLOCATOR_H */
|
@ -15,6 +15,9 @@
|
||||
/* NULL, size_t */
|
||||
#include <stddef.h>
|
||||
|
||||
/* allocator_t */
|
||||
#include <threadless/allocator.h>
|
||||
|
||||
/** Opaque coroutine type */
|
||||
typedef struct coroutine coroutine_t;
|
||||
|
||||
@ -31,15 +34,16 @@ extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/** Create a coroutine
|
||||
* @param function function to run in coroutine
|
||||
* @param stack_pages number of pages to map for coroutine stack
|
||||
* @param[in,out] allocator allocator to use to create/destroy memory
|
||||
* @param function function to run in coroutine
|
||||
* @param stack_size coroutine stack size
|
||||
* @retval non-NULL new coroutine
|
||||
* @retval NULL error (check @c errno for reason)
|
||||
* @post upon success, return value may be passed to coroutine_resume()
|
||||
* @post upon success, return value must be passed to coroutine_destroy()
|
||||
*/
|
||||
coroutine_t *coroutine_create(coroutine_function_t *function,
|
||||
size_t stack_pages);
|
||||
coroutine_t *coroutine_create(allocator_t *allocator,
|
||||
coroutine_function_t *function, size_t stack_size);
|
||||
|
||||
/** Destroy a coroutine
|
||||
* @param[in,out] coro coroutine to destroy
|
||||
|
Reference in New Issue
Block a user