Hash :
b72b719d
Author :
Thomas de Grivel
Date :
2024-12-26T04:46:52
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
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/mman.h>
#include "alloc.h"
#include "assert.h"
#include "skiplist__alloc.h"
#include "skiplist_node__alloc.h"
static sw g_alloc_init = 0;
static s_skiplist__alloc g_alloc_skiplist_mapped_size = {0};
static s_skiplist__alloc g_alloc_skiplist_size_mapped = {0};
void * alloc (uw size)
{
s_alloc *a;
if (! size) {
err_puts("alloc: zero size");
assert(! "alloc: zero size");
return NULL;
}
a = alloc_map(sizeof(s_alloc));
if (! a) {
err_puts("alloc: alloc_map(s_alloc)");
assert(! "alloc: alloc_map(s_alloc)");
return NULL;
}
a->size = size;
a->mapped = alloc_map(size);
if (! a->mapped) {
err_puts("alloc: alloc_map(size)");
assert(! "alloc: alloc_map(size)");
return NULL;
}
skiplist_insert__alloc(&g_alloc_skiplist_mapped_size, a);
skiplist_insert__alloc(&g_alloc_skiplist_size_mapped, a);
return a->mapped;
}
void alloc_clean (void)
{
g_alloc_init--;
if (! g_alloc_init) {
skiplist_clean__alloc(&g_alloc_skiplist_mapped_size);
skiplist_clean__alloc(&g_alloc_skiplist_size_mapped);
}
}
s8 alloc_init (void)
{
if (g_alloc_init) {
g_alloc_init++;
return 0;
}
if (! skiplist_init__alloc(&g_alloc_skiplist_mapped_size, 32, 2.5)) {
err_puts("alloc_init: skiplist_init__alloc 1");
assert(! "alloc_init: skiplist_init__alloc 1");
return 1;
}
if (! skiplist_init__alloc(&g_alloc_skiplist_size_mapped, 32, 2.5)) {
err_puts("alloc_init: skiplist_init__alloc 2");
assert(! "alloc_init: skiplist_init__alloc 2");
return 1;
}
g_alloc_init = 1;
return 0;
}
void alloc_free (void *allocated)
{
s_alloc *al;
s_skiplist_node__alloc *node;
s_skiplist_node__alloc *pred;
s_alloc pred_alloc = {0};
pred_alloc.mapped = allocated;
if (! (pred = skiplist_pred__alloc(&g_alloc_skiplist_mapped_size,
&pred_alloc)))
goto ko;
if (! (node = SKIPLIST_NODE_NEXT__alloc(pred, 0)))
goto ko;
al = node->alloc;
if (al->mapped != allocated)
goto ko;
alloc_unmap(allocated, al->size);
skiplist_remove__alloc(&g_alloc_skiplist_mapped_size, al);
skiplist_remove__alloc(&g_alloc_skiplist_size_mapped, al);
alloc_unmap(al, sizeof(s_alloc));
ko:
err_puts("free: invalid argument");
assert(! "free: invalid argument");
}
void * alloc_map (uw size)
{
sw e;
void *mapped = NULL;
if ((mapped = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0)) == MAP_FAILED) {
e = errno;
err_write_1("alloc_map: mmap: ");
err_write_1(strerror(e));
err_write_1("\n");
assert(! "alloc_map: mmap");
return NULL;
}
bzero(mapped, size);
return mapped;
}
void alloc_unmap (void *mapped, uw size)
{
sw e;
if (munmap(mapped, size) < 0) {
e = errno;
err_write_1("alloc_unmap: munmap: ");
err_write_1(strerror(e));
err_write_1("\n");
assert(! "alloc_unmap: munmap");
}
}