Skip to content
This repository
tree: 363e5ecfce
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 133 lines (120 sloc) 5.256 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Allocator_H
#define Allocator_H

#include <stdlib.h>
#include <stdbool.h>

struct Allocator;

/**
* Writer interface which writes data to memory and
* provides pointers to the memory locations where it wrote.
*/
struct Allocator
{
    /** The internal state of the Allocator. */
    void* const context;

    /**
* Free the heap allocations held by the allocator and any of it's children.
*
* @param this the Allocator which is being called. Use: allocator->free(allocator);
*/
    void (* const free)(const struct Allocator* this);

    /**
* Add a function to be called when the allocator is freed.
* This helps keep track of memory allocated by third party applications
* which demand to use malloc themselves.
*
* @param callback the function to call.
* @param callbackContext the data to pass the function when calling it.
* @param this the memory allocator.
* @return a pointer which can be passed to notOnFree() to remove the job.
*/
    void* (* const onFree)(void (*callback)(void* callbackContext),
                           void* callbackContext,
                           const struct Allocator* this);

    /**
* Remove a function which was registered with onFree().
*
* @param job the return value from calling onFree().
* @param alloc the memory allocator.
* @return true if the job was found and removed, false otherwise.
*/
    bool (* const notOnFree)(void* job, struct Allocator* alloc);

    /**
* Allocate some memory from this memory allocator.
* The allocation will be aligned on the size of a pointer, if you need further alignment then
* you must handle it manually.
*
* @param numberOfBytes how much memory to allocate.
* @param this the memory allocator, use allocator->malloc(10, allocator) to allocate 10 bytes.
* @return a pointer to the newly allocated memory.
* @see malloc()
*/
    void* (* const malloc)(size_t numberOfBytes,
                           const struct Allocator* this);

    /**
* Allocate some memory from this memory allocator.
* The allocation will be aligned on the size of a pointer, if you need further alignment then
* you must handle it manually.
* Memory location will be initialized to 0.
*
* @param numberOfBytes how much memory to allocate.
* @param multiplier how many times the number of bytes to allocate.
* @param this the memory allocator.
* @return a pointer to the newly allocated memory.
* @see calloc()
*/
    void* (* const calloc)(size_t numberOfBytes,
                           size_t multiplier,
                           const struct Allocator* this);

    /**
* Allocate some memory and copy something into that memory space.
* The allocation will be aligned on the size of a pointer, if you need further alignment then
* you must handle it manually.
*
* @param numberOfBytes how much memory to allocate.
* @param thisAllocator the memory allocator.
* @param toClone a pointer to something which will be cloned into the newly allocated memory,
* if this is NULL or is not as large as numberOfBytes, undefined behavior will
* result.
* @return a pointer to the newly allocated memory.
*/
    void* (* const clone)(size_t numberOfBytes,
                          const struct Allocator* thisAllocator,
                          const void* toClone);

    /**
* Re-allocate memory so that an allocation can be expanded.
* The allocation will be aligned on the size of a pointer, if you need further alignment then
* you must handle it manually.
*
* @param originalAllocation a pointer to the original memory allocation which is to be
* reallocated.
* @param numberOfBytes how much memory to allocate.
* @param thisAllocator the memory allocator.
* @return a pointer to the newly allocated memory.
*/
    void* (* const realloc)(const void* originalAllocation,
                            size_t numberOfBytes,
                            const struct Allocator* thisAllocator);

    /**
* Get a new child of this allocator.
* When this allocator is freed all of it's children will be freed as well.
*
* @param this the memory allocator, use allocator->child(allocator) to get a child.
* @return a child allocator.
*/
    struct Allocator* (* const child)(const struct Allocator* this);
};

#endif /* MEMALLOCATOR_H */
Something went wrong with that request. Please try again.