kore/src/mem.c

285 lines
5.3 KiB
C
Raw Normal View History

/*
2019-02-22 16:57:28 +01:00
* Copyright (c) 2013-2019 Joris Vink <joris@coders.se>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <stdlib.h>
2014-07-22 09:53:44 +02:00
#include <stdint.h>
#include "kore.h"
#define KORE_MEM_BLOCKS 11
#define KORE_MEM_BLOCK_SIZE_MAX 8192
#define KORE_MEM_BLOCK_PREALLOC 128
#define KORE_MEM_ALIGN (sizeof(size_t))
#define KORE_MEM_MAGIC 0xd0d0
#define KORE_MEMSIZE(x) \
(*(size_t *)((u_int8_t *)x - sizeof(size_t)))
#define KORE_MEMINFO(x) \
(struct meminfo *)((u_int8_t *)x + KORE_MEMSIZE(x))
#define KORE_MEM_TAGGED 0x0001
struct meminfo {
u_int16_t flags;
2013-06-27 12:27:17 +02:00
u_int16_t magic;
};
struct memblock {
struct kore_pool pool;
};
struct tag {
void *ptr;
u_int32_t id;
TAILQ_ENTRY(tag) list;
};
static size_t memblock_index(size_t);
static TAILQ_HEAD(, tag) tags;
static struct kore_pool tag_pool;
static struct memblock blocks[KORE_MEM_BLOCKS];
void
kore_mem_init(void)
{
int i, len;
char name[32];
u_int32_t size, elm, mlen;
size = 8;
TAILQ_INIT(&tags);
kore_pool_init(&tag_pool, "tag_pool", sizeof(struct tag), 100);
for (i = 0; i < KORE_MEM_BLOCKS; i++) {
len = snprintf(name, sizeof(name), "block-%u", size);
if (len == -1 || (size_t)len >= sizeof(name))
fatal("kore_mem_init: snprintf");
elm = (KORE_MEM_BLOCK_PREALLOC * 1024) / size;
mlen = sizeof(size_t) + size +
sizeof(struct meminfo) + KORE_MEM_ALIGN;
mlen = mlen & ~(KORE_MEM_ALIGN - 1);
kore_pool_init(&blocks[i].pool, name, mlen, elm);
size = size << 1;
}
}
void
kore_mem_cleanup(void)
{
int i;
for (i = 0; i < KORE_MEM_BLOCKS; i++) {
kore_pool_cleanup(&blocks[i].pool);
}
}
void *
kore_malloc(size_t len)
{
void *ptr;
struct meminfo *mem;
u_int8_t *addr;
size_t mlen, idx, *plen;
if (len == 0)
len = 8;
if (len <= KORE_MEM_BLOCK_SIZE_MAX) {
idx = memblock_index(len);
ptr = kore_pool_get(&blocks[idx].pool);
} else {
mlen = sizeof(size_t) + len + sizeof(struct meminfo);
if ((ptr = calloc(1, mlen)) == NULL)
2018-07-18 16:09:05 +02:00
fatal("kore_malloc(%zu): %d", len, errno);
}
plen = (size_t *)ptr;
*plen = len;
addr = (u_int8_t *)ptr + sizeof(size_t);
mem = KORE_MEMINFO(addr);
mem->flags = 0;
mem->magic = KORE_MEM_MAGIC;
return (addr);
}
void *
kore_realloc(void *ptr, size_t len)
{
struct meminfo *mem;
void *nptr;
if (ptr == NULL) {
nptr = kore_malloc(len);
} else {
if (len == KORE_MEMSIZE(ptr))
return (ptr);
mem = KORE_MEMINFO(ptr);
if (mem->magic != KORE_MEM_MAGIC)
fatal("kore_realloc(): magic boundary not found");
nptr = kore_malloc(len);
memcpy(nptr, ptr, MIN(len, KORE_MEMSIZE(ptr)));
kore_free(ptr);
}
return (nptr);
}
void *
kore_calloc(size_t memb, size_t len)
{
void *ptr;
size_t total;
2015-12-29 22:29:35 +01:00
if (SIZE_MAX / memb < len)
2016-02-13 15:45:33 +01:00
fatal("kore_calloc(): memb * len > SIZE_MAX");
total = memb * len;
ptr = kore_malloc(total);
memset(ptr, 0, total);
return (ptr);
}
void
kore_free(void *ptr)
{
u_int8_t *addr;
struct meminfo *mem;
size_t len, idx;
if (ptr == NULL)
return;
mem = KORE_MEMINFO(ptr);
if (mem->magic != KORE_MEM_MAGIC)
fatal("kore_free(): magic boundary not found");
if (mem->flags & KORE_MEM_TAGGED) {
kore_mem_untag(ptr);
mem->flags &= ~KORE_MEM_TAGGED;
}
len = KORE_MEMSIZE(ptr);
addr = (u_int8_t *)ptr - sizeof(size_t);
if (len <= KORE_MEM_BLOCK_SIZE_MAX) {
idx = memblock_index(len);
kore_pool_put(&blocks[idx].pool, addr);
} else {
free(addr);
}
}
char *
kore_strdup(const char *str)
{
size_t len;
char *nstr;
len = strlen(str) + 1;
nstr = kore_malloc(len);
(void)kore_strlcpy(nstr, str, len);
return (nstr);
}
void *
kore_malloc_tagged(size_t len, u_int32_t tag)
{
void *ptr;
ptr = kore_malloc(len);
kore_mem_tag(ptr, tag);
return (ptr);
}
void
kore_mem_tag(void *ptr, u_int32_t id)
{
struct tag *tag;
struct meminfo *mem;
if (kore_mem_lookup(id) != NULL)
fatal("kore_mem_tag: tag %u taken", id);
mem = KORE_MEMINFO(ptr);
if (mem->magic != KORE_MEM_MAGIC)
fatal("kore_mem_tag: magic boundary not found");
mem->flags |= KORE_MEM_TAGGED;
tag = kore_pool_get(&tag_pool);
tag->id = id;
tag->ptr = ptr;
TAILQ_INSERT_TAIL(&tags, tag, list);
}
void
kore_mem_untag(void *ptr)
{
struct tag *tag;
TAILQ_FOREACH(tag, &tags, list) {
if (tag->ptr == ptr) {
TAILQ_REMOVE(&tags, tag, list);
kore_pool_put(&tag_pool, tag);
break;
}
}
}
void *
kore_mem_lookup(u_int32_t id)
{
struct tag *tag;
TAILQ_FOREACH(tag, &tags, list) {
if (tag->id == id)
return (tag->ptr);
}
return (NULL);
}
static size_t
memblock_index(size_t len)
{
size_t mlen, idx;
idx = 0;
mlen = 8;
while (mlen < len) {
idx++;
mlen = mlen << 1;
}
if (idx > (KORE_MEM_BLOCKS - 1))
fatal("kore_malloc: idx too high");
return (idx);
}