summaryrefslogtreecommitdiffstats
path: root/include/pool.h
blob: 7121f183701ad7dc2b88b075a6d981829f1245b8 (plain)
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
// pool.h - Fixed block size, free list based, memory pool
//
// Note: block_s must be >= sizeof(void*) and aligned to maintain proper
// pointer alignment for returned blocks.
//
// Thread safety: Not thread-safe. External synchronization required.
// Memory safety: pool_free validates pointer range, alignment, and detects
//   double-free via free-list walk (O(n) in number of free blocks).
//   Invalid frees are silently ignored.

#ifndef POOL_H
#define POOL_H

#include "type.h"

typedef struct pool_t {
  u32 block_n; // Block capacity
  u32 block_i; // Block count
  u32 block_s; // Block size in bytes
  void *data;   // Blocks
  void *free;   // Free chunk
} pool_t;

pool_t *pool_init(u32, u32);
void pool_fini(pool_t **);
void *pool_malloc(pool_t *);
void pool_free(pool_t *, void *);

u32 pool_capacity(const pool_t *);
u32 pool_size(const pool_t *);

#endif // POOL_H

#ifdef POOL_IMPLEMENTATION // IMPLEMENTATION

#include <stddef.h>
#include <assert.h>
#include <stdlib.h>

static void pool_free_list(uintptr_t *, u32, u32);

// pool_init()
//
// Initializes a memory pool and returns a pointer to the memory pool
//
// block_s - The size of a block in bytes
// block_n - The number of initial blocks

pool_t *pool_init(u32 block_s, u32 block_n) {
  if (block_n == 0)
    return NULL;
  if (block_s < sizeof(void *) || block_s % sizeof(void *) != 0)
    return NULL;

  pool_t *pool = (pool_t *)malloc(sizeof(pool_t));
  if (!pool)
    return NULL;

  // Set size and count
  pool->block_n = block_n;
  pool->block_s = block_s;
  pool->block_i = 0;

  if (block_n != 0 && block_s > SIZE_MAX / (size_t)block_n) {
    free(pool);
    return NULL;
  }
  pool->data = calloc(block_n, block_s);
  if (!pool->data) {
    free(pool);
    return NULL;
  }

  pool->free = pool->data;
  pool_free_list((uintptr_t *)pool->data, block_n, block_s);

  return pool;
}

// pool_malloc()
//
// Return a block of free memory
//
// pool - A pointer to the pool object

void *pool_malloc(pool_t *pool) {
  if (!pool)
    return NULL;
  uintptr_t *free = (uintptr_t *)pool->free;

  if (free) {
    pool->free = (uintptr_t *)*free;
    pool->block_i++;
  }

  return free;
}

// pool_free()
//
// Free the memory at the pointer p
//
// pool - A pointer to the pool object

void pool_free(pool_t *pool, void *p) {
  if (!pool || !p || !pool->data)
    return;
  // Validate pointer is within pool range
  u8 *base = (u8 *)pool->data;
  u8 *ptr = (u8 *)p;
  if ((u64)pool->block_n > UINT64_MAX / (u64)pool->block_s)
    return;
  u64 total = (u64)pool->block_n * pool->block_s;
  if (total > SIZE_MAX)
    return;
  u8 *end = base + (size_t)total;
  if (ptr < base || ptr >= end)
    return;
  // Validate pointer is aligned to block size
  if ((ptr - base) % pool->block_s != 0)
    return;
  // Check for double-free: walk free list to detect already-freed block
  {
    uintptr_t *cur = (uintptr_t *)pool->free;
    while (cur) {
      if ((uintptr_t)cur == (uintptr_t)p)
        return;
      cur = (uintptr_t *)*cur;
    }
  }
  // Convert the pointer into a usable type
  uintptr_t *free = (uintptr_t *)p;

  // Set the value at the address to the current front of the free list
  *free = (uintptr_t)pool->free;

  // Set the pool's free head to the newly freed pointer
  pool->free = (void *)free;

  if (pool->block_i > 0)
    pool->block_i--;
}

u32 pool_capacity(const pool_t *pool) { if (!pool) return 0; return pool->block_n; }

u32 pool_size(const pool_t *pool) { if (!pool) return 0; return pool->block_i; }

static void pool_free_list(uintptr_t *block, u32 block_n,
                             u32 block_s) {
  if (block_n == 0)
    return;

  u8 *ptr = (u8 *)block;
  for (u32 i = 0; i < block_n - 1; i++) {
    uintptr_t *tmp = (uintptr_t *)ptr;
    *tmp = (uintptr_t)ptr + block_s;
    ptr = ptr + block_s;
  }

  *(uintptr_t *)ptr = (uintptr_t)0;
}

void pool_fini(pool_t **pool) {
  if (pool && *pool) {
    free((*pool)->data);
    free(*pool);
    *pool = NULL;
  }
}

#endif // POOL_IMPLEMENTATION