Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 235 lines (205 sloc) 9.407 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */

#if !defined(_memory_pool_h_)
#define _memory_pool_h_

#include <stdlib.h>

typedef enum memory_pool_e {
#define MEMORY_POOL(pool_enum, pool_counter, pool_string) pool_enum,
#include "memory_pool_classes.h"
} memory_pool_t;


typedef struct pool_prefix_s pool_prefix_t;
struct pool_prefix_s {
    void* ptr;
    size_t pool_length;
    memory_pool_t pool;
    uint32_t signature;
};

#define MEMORY_POOL_SIGNATURE 0xdeadcafe
// #define MEMORY_POOL_CHECKS
// #define MEMORY_POOL_ERROR_BREAKDOWN

#define pool_malloc(bytes, pool) pool_malloc_locking(true, bytes, pool)
#define pool_calloc(nmemb, size, pool) pool_calloc_locking(true, nmemb, size, pool)
#define pool_free(ptr, bytes, pool) pool_free_locking(true, ptr, bytes, pool)
#define pool_realloc(ptr, bytes, previous, pool) pool_realloc_locking(true, ptr, bytes, previous, pool)

#if defined(NDEBUG)
#define pool_assert(counter, assertion) \
if (! (assertion)) { \
STATS_LOCK(stats); \
stats->counter ++; \
STATS_UNLOCK(stats); \
}
#else
#define pool_assert(counter, assertion) assert(assertion)
#endif /* #if defined(NDEBUG) */


static inline void* pool_malloc_locking(bool do_lock, size_t bytes, memory_pool_t pool) {
    stats_t *stats = STATS_GET_TLS();
#if defined(MEMORY_POOL_CHECKS)
    void* alloc = malloc(bytes + sizeof(pool_prefix_t));
    pool_prefix_t* prefix = alloc;
    void* ret = alloc + sizeof(pool_prefix_t);
    prefix->ptr = ret;
    prefix->pool_length = bytes;
    prefix->pool = pool;
    prefix->signature = MEMORY_POOL_SIGNATURE;
#else /* #if defined(MEMORY_POOL_CHECKS) */
    void* ret = malloc(bytes);
#endif /* #if defined(MEMORY_POOL_CHECKS) */

    if (ret != NULL) {
        switch (pool) {
#define MEMORY_POOL(pool_enum, pool_counter, pool_string) \
case pool_enum: \
if (do_lock) \
STATS_LOCK(stats); \
stats->pool_counter += bytes; \
if (do_lock) \
STATS_UNLOCK(stats); \
break;
#include "memory_pool_classes.h"
        }
    }

    return ret;
}


static inline void* pool_calloc_locking(bool do_lock, size_t nmemb, size_t size, memory_pool_t pool) {
    stats_t *stats = STATS_GET_TLS();
    size_t bytes = nmemb * size;
#if defined(MEMORY_POOL_CHECKS)
    void* alloc = malloc(bytes + sizeof(pool_prefix_t));
    pool_prefix_t* prefix = alloc;
    void* ret = alloc + sizeof(pool_prefix_t);
    prefix->ptr = ret;
    prefix->pool_length = bytes;
    prefix->pool = pool;
    prefix->signature = MEMORY_POOL_SIGNATURE;
    memset(ret, 0, bytes);
#else /* #if defined(MEMORY_POOL_CHECKS) */
    void* ret = calloc(nmemb, size);
#endif /* #if defined(MEMORY_POOL_CHECKS) */

    if (ret != NULL) {
        switch (pool) {
#define MEMORY_POOL(pool_enum, pool_counter, pool_string) \
case pool_enum: \
if (do_lock) \
STATS_LOCK(stats); \
stats->pool_counter += bytes; \
if (do_lock) \
STATS_UNLOCK(stats); \
break;
#include "memory_pool_classes.h"
        }
    }

    return ret;
}


static inline void pool_free_locking(bool do_lock, void* ptr, size_t previous, memory_pool_t pool) {
    stats_t *stats = STATS_GET_TLS();
#if defined(MEMORY_POOL_CHECKS)
    pool_prefix_t* prefix = ptr;
    void* alloc;
#if defined(MEMORY_POOL_ERROR_BREAKDOWN)
    size_t saved_pool_length;
#endif /* #if defined(MEMORY_POOL_ERROR_BREAKDOWN) */

    prefix --;
    alloc = prefix;

#if defined(MEMORY_POOL_ERROR_BREAKDOWN)
    saved_pool_length = prefix->pool_length;
#endif /* #if defined(MEMORY_POOL_ERROR_BREAKDOWN) */

    pool_assert(mp_blk_errors, prefix->ptr == ptr);
    pool_assert(mp_bytecount_errors, prefix->pool_length == previous);
    pool_assert(mp_pool_errors, prefix->pool == pool);
    pool_assert(mp_blk_errors, prefix->signature == MEMORY_POOL_SIGNATURE);
    free(alloc);
#else /* #if defined(MEMORY_POOL_CHECKS) */
    free(ptr);
#endif /* #if defined(MEMORY_POOL_CHECKS) */

    switch (pool) {
#if defined(MEMORY_POOL_ERROR_BREAKDOWN)
#define MEMORY_POOL(pool_enum, pool_counter, pool_string) \
case pool_enum: \
if (do_lock) { \
STATS_LOCK(stats); \
} \
stats->pool_counter -= previous; \
if (saved_pool_length != previous) { \
stats->mp_bytecount_errors_free_split.pool_counter ++; \
} \
if (do_lock) { \
STATS_UNLOCK(stats); \
} \
break;
#else
#define MEMORY_POOL(pool_enum, pool_counter, pool_string) \
case pool_enum: \
if (do_lock) { \
STATS_LOCK(stats); \
} \
stats->pool_counter -= previous; \
if (do_lock) { \
STATS_UNLOCK(stats); \
} \
break;
#endif /* #if defined(MEMORY_POOL_ERROR_BREAKDOWN) */
#include "memory_pool_classes.h"
    }
}


static inline void* pool_realloc_locking(bool do_lock, void* ptr, size_t bytes, size_t previous, memory_pool_t pool) {
    stats_t *stats = STATS_GET_TLS();
#if defined(MEMORY_POOL_CHECKS)
    pool_prefix_t* prefix = ptr;
    void* alloc, * ret;
#if defined(MEMORY_POOL_ERROR_BREAKDOWN)
    size_t saved_pool_length;
#endif /* #if defined(MEMORY_POOL_ERROR_BREAKDOWN) */

    prefix --;
    alloc = prefix;

#if defined(MEMORY_POOL_ERROR_BREAKDOWN)
    saved_pool_length = prefix->pool_length;
#endif /* #if defined(MEMORY_POOL_ERROR_BREAKDOWN) */

    pool_assert(mp_blk_errors, prefix->ptr == ptr);
    pool_assert(mp_bytecount_errors, prefix->pool_length == previous);
    pool_assert(mp_pool_errors, prefix->pool == pool);
    pool_assert(mp_blk_errors, prefix->signature == MEMORY_POOL_SIGNATURE);
    alloc = realloc(alloc, bytes + sizeof(pool_prefix_t));
    if (alloc == NULL) {
        return NULL;
    }
    prefix = alloc;
    ret = alloc + sizeof(pool_prefix_t);
    prefix->ptr = ret;
    prefix->pool_length = bytes;
#else /* #if defined(MEMORY_POOL_CHECKS) */
    void* ret = realloc(ptr, bytes);
#endif /* #if defined(MEMORY_POOL_CHECKS) */

    if (ret != NULL) {
        switch (pool) {
#if defined(MEMORY_POOL_ERROR_BREAKDOWN)
#define MEMORY_POOL(pool_enum, pool_counter, pool_string) \
case pool_enum: \
if (do_lock) { \
STATS_LOCK(stats); \
} \
stats->pool_counter -= previous; \
stats->pool_counter += bytes; \
if (saved_pool_length != previous) { \
stats->mp_bytecount_errors_realloc_split.pool_counter ++; \
} \
if (do_lock) { \
STATS_UNLOCK(stats); \
} \
break;
#else
#define MEMORY_POOL(pool_enum, pool_counter, pool_string) \
case pool_enum: \
if (do_lock) { \
STATS_LOCK(stats); \
} \
stats->pool_counter -= previous; \
stats->pool_counter += bytes; \
if (do_lock) { \
STATS_UNLOCK(stats); \
} \
break;
#endif /* #if defined(MEMORY_POOL_ERROR_BREAKDOWN) */
#include "memory_pool_classes.h"
        }
    }

    return ret;
}

#endif /* #if !defined(_memory_pool_h_) */
Something went wrong with that request. Please try again.