Debian Codegraph @

libmowgli-2/src/libmowgli/core/heap.disabled.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * heap.c: Heap allocation.
4 *
5 * Copyright (c) 2019 Aaron M. D. Jones <[email protected]>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice is present in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #include "mowgli.h"
25
26 struct mowgli_heap_
27 {
28 mowgli_list_t blocks; // retained for ABI compatibility
29 mowgli_allocation_policy_t *allocator;
30 size_t elem_size;
31 };
32
33 mowgli_heap_t *
34 mowgli_heap_create_full(const size_t elem_size,
35 const size_t MOWGLI_VATTR_UNUSED mowgli_heap_elems,
36 const unsigned int MOWGLI_VATTR_UNUSED flags,
37 mowgli_allocation_policy_t *restrict allocator)
38 {
39 return_null_if_fail(elem_size != 0);
40
41 if (! allocator)
42 allocator = mowgli_allocator_get_policy();
43
44 return_null_if_fail(allocator != NULL);
45 return_null_if_fail(allocator->allocate != NULL);
46
47 mowgli_heap_t *const heap = allocator->allocate(sizeof *heap);
48
49 if (! heap)
50 return NULL;
51
52 (void) memset(heap, 0x00, sizeof *heap);
53
54 heap->allocator = allocator;
55 heap->elem_size = elem_size;
56
57 return heap;
58 }
59
60 mowgli_heap_t *
61 mowgli_heap_create(const size_t elem_size,
62 const size_t MOWGLI_VATTR_UNUSED mowgli_heap_elems,
63 const unsigned int MOWGLI_VATTR_UNUSED flags)
64 {
65 return_null_if_fail(elem_size != 0);
66
67 return mowgli_heap_create_full(elem_size, 0, 0, NULL);
68 }
69
70 void
71 mowgli_heap_destroy(mowgli_heap_t *const restrict heap)
72 {
73 return_if_fail(heap != NULL);
74 return_if_fail(heap->allocator != NULL);
75 return_if_fail(heap->allocator->deallocate != NULL);
76
77 (void) heap->allocator->deallocate(heap);
78 }
79
80 void *
81 mowgli_heap_alloc(mowgli_heap_t *const restrict heap)
82 {
83 return_null_if_fail(heap != NULL);
84 return_null_if_fail(heap->allocator != NULL);
85 return_null_if_fail(heap->allocator->allocate != NULL);
86 return_null_if_fail(heap->allocator->deallocate != NULL);
87
88 void *ptr = heap->allocator->allocate(heap->elem_size);
89
90 if (!ptr)
91 return NULL;
92
93 return memset(ptr, 0x00, heap->elem_size);
94 }
95
96 void
97 mowgli_heap_free(mowgli_heap_t *const restrict heap, void *const restrict ptr)
98 {
99 return_if_fail(heap != NULL);
100 return_if_fail(heap->allocator != NULL);
101 return_if_fail(heap->allocator->deallocate != NULL);
102 return_if_fail(ptr != NULL);
103
104 (void) heap->allocator->deallocate(ptr);
105 }