liballoc-c

C generic allocator interface
git clone https://git.sinitax.com/sinitax/liballoc-c
Log | Files | Refs | LICENSE | sfeed.txt

allocator.c (3213B)


      1#include "allocator.h"
      2
      3#include <errno.h>
      4#include <stdlib.h>
      5
      6static void *stdlib_heap_alloc(const struct allocator *allocator,
      7	size_t size, int *rc);
      8static void *stdlib_heap_realloc(const struct allocator *allocator,
      9	void *p, size_t size, int *rc);
     10static int stdlib_heap_free(const struct allocator *allocator, void *p);
     11
     12static void *stdlib_strict_heap_alloc(const struct allocator *allocator,
     13	size_t size, int *rc);
     14static void *stdlib_strict_heap_realloc(const struct allocator *allocator,
     15	void *p, size_t size, int *rc);
     16static int stdlib_strict_heap_free(const struct allocator *allocator, void *p);
     17
     18const struct allocator stdlib_heap_allocator = {
     19	.alloc = stdlib_heap_alloc,
     20	.realloc = stdlib_heap_realloc,
     21	.free = stdlib_heap_free
     22};
     23
     24const struct allocator stdlib_strict_heap_allocator = {
     25	.alloc = stdlib_strict_heap_alloc,
     26	.realloc = stdlib_strict_heap_realloc,
     27	.free = stdlib_strict_heap_free
     28};
     29
     30void *
     31stdlib_heap_alloc(const struct allocator *allocator, size_t size, int *rc)
     32{
     33	void *p;
     34
     35	p = malloc(size);
     36	if (rc && !p) *rc = errno;
     37
     38	return p;
     39}
     40
     41void *
     42stdlib_heap_realloc(const struct allocator *allocator,
     43	void *p, size_t size, int *rc)
     44{
     45	void *np;
     46
     47	np = realloc(p, size);
     48	if (rc && !np) *rc = errno;
     49
     50	return np;
     51}
     52
     53int
     54stdlib_heap_free(const struct allocator *allocator, void *p)
     55{
     56	free(p);
     57
     58	return 0;
     59}
     60
     61void *
     62stdlib_strict_heap_alloc(const struct allocator *allocator,
     63	size_t size, int *rc)
     64{
     65	void *p;
     66
     67	p = malloc(size);
     68	if (!p) abort();
     69
     70	return p;
     71}
     72
     73void *
     74stdlib_strict_heap_realloc(const struct allocator *allocator,
     75	void *p, size_t size, int *rc)
     76{
     77	void *np;
     78
     79	np = realloc(p, size);
     80	if (!np) abort();
     81
     82	return np;
     83}
     84
     85int
     86stdlib_strict_heap_free(const struct allocator *allocator, void *p)
     87{
     88	free(p);
     89
     90	return 0;
     91}
     92
     93void *
     94strict_allocator_alloc(const struct allocator *allocator, size_t size, int *rc)
     95{
     96	const struct strict_allocator *strict_allocator;
     97	void *p;
     98
     99	strict_allocator = ((void *) allocator)
    100		- offsetof(struct strict_allocator, allocator);
    101
    102	p = strict_allocator->allocator_ul->alloc(
    103		strict_allocator->allocator_ul, size, rc);
    104	if (!p) abort();
    105
    106	return p;
    107}
    108
    109void *
    110strict_allocator_realloc(const struct allocator *allocator,
    111	void *p, size_t size, int *rc)
    112{
    113	const struct strict_allocator *strict_allocator;
    114	void *np;
    115
    116	strict_allocator = ((void *) allocator)
    117		- offsetof(struct strict_allocator, allocator);
    118
    119	np = strict_allocator->allocator_ul->realloc(
    120		strict_allocator->allocator_ul, p, size, rc);
    121	if (!np) abort();
    122
    123	return np;
    124}
    125
    126int
    127strict_allocator_free(const struct allocator *allocator, void *p)
    128{
    129	const struct strict_allocator *strict_allocator;
    130	int rc;
    131
    132	strict_allocator = ((void *) allocator)
    133		- offsetof(struct strict_allocator, allocator);
    134
    135	rc = strict_allocator->allocator_ul->free(
    136		strict_allocator->allocator_ul, p);
    137	if (rc) abort();
    138
    139	return 0;
    140}
    141
    142void
    143strict_allocator_init(struct strict_allocator *strict_allocator,
    144	const struct allocator *allocator_ul)
    145{
    146	strict_allocator->allocator_ul = allocator_ul;
    147	strict_allocator->allocator.alloc = strict_allocator_alloc;
    148	strict_allocator->allocator.realloc = strict_allocator_realloc;
    149	strict_allocator->allocator.free = strict_allocator_free;
    150}