2013-06-27 08:43:07 +02:00
|
|
|
/*
|
2021-01-11 23:46:08 +01:00
|
|
|
* Copyright (c) 2013-2021 Joris Vink <joris@coders.se>
|
2013-06-27 08:43:07 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-07-22 22:44:42 +02:00
|
|
|
#include <sys/param.h>
|
2019-03-06 09:29:46 +01:00
|
|
|
#include <sys/types.h>
|
2013-07-22 22:44:42 +02:00
|
|
|
|
2013-07-13 21:47:04 +02:00
|
|
|
#include <stdlib.h>
|
2014-07-22 09:53:44 +02:00
|
|
|
#include <stdint.h>
|
2013-07-13 21:47:04 +02:00
|
|
|
|
2013-06-27 08:43:07 +02:00
|
|
|
#include "kore.h"
|
|
|
|
|
2016-07-12 13:54:14 +02:00
|
|
|
#define KORE_MEM_BLOCKS 11
|
|
|
|
#define KORE_MEM_BLOCK_SIZE_MAX 8192
|
|
|
|
#define KORE_MEM_BLOCK_PREALLOC 128
|
|
|
|
|
2019-10-15 21:33:43 +02:00
|
|
|
#define KORE_MEM_ALIGN 16
|
2013-06-27 08:43:07 +02:00
|
|
|
#define KORE_MEM_MAGIC 0xd0d0
|
|
|
|
|
2017-08-30 11:47:26 +02:00
|
|
|
#define KORE_MEM_TAGGED 0x0001
|
|
|
|
|
2019-10-15 21:33:43 +02:00
|
|
|
struct memsize {
|
|
|
|
size_t len;
|
|
|
|
size_t magic;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
2013-06-27 08:43:07 +02:00
|
|
|
struct meminfo {
|
2017-08-30 11:47:26 +02:00
|
|
|
u_int16_t flags;
|
2013-06-27 12:27:17 +02:00
|
|
|
u_int16_t magic;
|
2019-10-15 21:33:43 +02:00
|
|
|
} __attribute__((packed));
|
2013-06-27 08:43:07 +02:00
|
|
|
|
2016-07-12 13:54:14 +02:00
|
|
|
struct memblock {
|
|
|
|
struct kore_pool pool;
|
|
|
|
};
|
|
|
|
|
2017-08-30 11:47:26 +02:00
|
|
|
struct tag {
|
|
|
|
void *ptr;
|
|
|
|
u_int32_t id;
|
|
|
|
TAILQ_ENTRY(tag) list;
|
|
|
|
};
|
|
|
|
|
2019-10-15 21:33:43 +02:00
|
|
|
static inline struct memsize *memsize(void *);
|
|
|
|
static inline struct meminfo *meminfo(void *);
|
2016-07-12 13:54:14 +02:00
|
|
|
static size_t memblock_index(size_t);
|
|
|
|
|
2017-08-30 11:47:26 +02:00
|
|
|
static TAILQ_HEAD(, tag) tags;
|
|
|
|
static struct kore_pool tag_pool;
|
2016-07-12 13:54:14 +02:00
|
|
|
static struct memblock blocks[KORE_MEM_BLOCKS];
|
|
|
|
|
2013-06-27 08:43:07 +02:00
|
|
|
void
|
|
|
|
kore_mem_init(void)
|
|
|
|
{
|
2016-07-12 13:54:14 +02:00
|
|
|
int i, len;
|
|
|
|
char name[32];
|
|
|
|
u_int32_t size, elm, mlen;
|
|
|
|
|
|
|
|
size = 8;
|
2017-08-30 11:47:26 +02:00
|
|
|
TAILQ_INIT(&tags);
|
|
|
|
kore_pool_init(&tag_pool, "tag_pool", sizeof(struct tag), 100);
|
2016-07-12 13:54:14 +02:00
|
|
|
|
|
|
|
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;
|
2019-10-15 21:33:43 +02:00
|
|
|
mlen = sizeof(struct memsize) + size +
|
2016-07-28 14:52:15 +02:00
|
|
|
sizeof(struct meminfo) + KORE_MEM_ALIGN;
|
|
|
|
mlen = mlen & ~(KORE_MEM_ALIGN - 1);
|
|
|
|
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_pool_init(&blocks[i].pool, name, mlen, elm);
|
|
|
|
|
|
|
|
size = size << 1;
|
|
|
|
}
|
2013-06-27 08:43:07 +02:00
|
|
|
}
|
|
|
|
|
2017-02-06 11:42:23 +01:00
|
|
|
void
|
|
|
|
kore_mem_cleanup(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < KORE_MEM_BLOCKS; i++) {
|
|
|
|
kore_pool_cleanup(&blocks[i].pool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 08:43:07 +02:00
|
|
|
void *
|
|
|
|
kore_malloc(size_t len)
|
|
|
|
{
|
2013-07-13 22:19:50 +02:00
|
|
|
void *ptr;
|
2013-06-27 08:43:07 +02:00
|
|
|
struct meminfo *mem;
|
2019-10-15 21:33:43 +02:00
|
|
|
struct memsize *size;
|
2013-07-13 22:19:50 +02:00
|
|
|
u_int8_t *addr;
|
2019-10-15 21:33:43 +02:00
|
|
|
size_t mlen, idx;
|
2013-06-27 08:43:07 +02:00
|
|
|
|
2014-04-23 14:26:28 +02:00
|
|
|
if (len == 0)
|
2017-01-25 22:07:08 +01:00
|
|
|
len = 8;
|
2014-04-23 14:26:28 +02:00
|
|
|
|
2016-07-12 13:54:14 +02:00
|
|
|
if (len <= KORE_MEM_BLOCK_SIZE_MAX) {
|
|
|
|
idx = memblock_index(len);
|
|
|
|
ptr = kore_pool_get(&blocks[idx].pool);
|
|
|
|
} else {
|
2019-10-15 21:33:43 +02:00
|
|
|
mlen = sizeof(struct memsize) + len + sizeof(struct meminfo);
|
2016-07-12 13:54:14 +02:00
|
|
|
if ((ptr = calloc(1, mlen)) == NULL)
|
2018-07-18 16:09:05 +02:00
|
|
|
fatal("kore_malloc(%zu): %d", len, errno);
|
2016-07-12 13:54:14 +02:00
|
|
|
}
|
2013-06-27 08:43:07 +02:00
|
|
|
|
2019-10-15 21:33:43 +02:00
|
|
|
size = (struct memsize *)ptr;
|
|
|
|
size->len = len;
|
|
|
|
size->magic = KORE_MEM_MAGIC;
|
|
|
|
|
|
|
|
addr = (u_int8_t *)ptr + sizeof(struct memsize);
|
2013-07-13 22:19:50 +02:00
|
|
|
|
2019-10-15 21:33:43 +02:00
|
|
|
mem = (struct meminfo *)(addr + size->len);
|
2017-08-30 11:47:26 +02:00
|
|
|
mem->flags = 0;
|
2013-06-27 08:43:07 +02:00
|
|
|
mem->magic = KORE_MEM_MAGIC;
|
|
|
|
|
2013-07-13 22:19:50 +02:00
|
|
|
return (addr);
|
2013-06-27 08:43:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
kore_realloc(void *ptr, size_t len)
|
|
|
|
{
|
2019-10-15 21:33:43 +02:00
|
|
|
struct memsize *size;
|
2013-06-27 08:43:07 +02:00
|
|
|
void *nptr;
|
|
|
|
|
2013-06-27 09:20:48 +02:00
|
|
|
if (ptr == NULL) {
|
|
|
|
nptr = kore_malloc(len);
|
|
|
|
} else {
|
2019-10-15 21:33:43 +02:00
|
|
|
size = memsize(ptr);
|
|
|
|
if (len == size->len)
|
2017-01-25 22:07:08 +01:00
|
|
|
return (ptr);
|
2013-06-27 09:20:48 +02:00
|
|
|
nptr = kore_malloc(len);
|
2019-10-15 21:33:43 +02:00
|
|
|
memcpy(nptr, ptr, MIN(len, size->len));
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(ptr);
|
2013-06-27 09:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (nptr);
|
2013-06-27 08:43:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
kore_calloc(size_t memb, size_t len)
|
|
|
|
{
|
2017-02-27 19:46:59 -08:00
|
|
|
void *ptr;
|
|
|
|
size_t total;
|
|
|
|
|
2015-12-29 21:29:35 +00:00
|
|
|
if (SIZE_MAX / memb < len)
|
2016-02-13 15:45:33 +01:00
|
|
|
fatal("kore_calloc(): memb * len > SIZE_MAX");
|
2014-04-23 14:26:28 +02:00
|
|
|
|
2017-02-27 19:46:59 -08:00
|
|
|
total = memb * len;
|
|
|
|
ptr = kore_malloc(total);
|
|
|
|
memset(ptr, 0, total);
|
|
|
|
|
|
|
|
return (ptr);
|
2013-06-27 08:43:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(void *ptr)
|
2013-06-27 08:43:07 +02:00
|
|
|
{
|
2019-10-15 21:33:43 +02:00
|
|
|
size_t idx;
|
2016-07-12 13:54:14 +02:00
|
|
|
struct meminfo *mem;
|
2019-10-15 21:33:43 +02:00
|
|
|
struct memsize *size;
|
|
|
|
u_int8_t *addr;
|
2013-06-27 08:43:07 +02:00
|
|
|
|
2014-09-25 22:20:47 +02:00
|
|
|
if (ptr == NULL)
|
|
|
|
return;
|
|
|
|
|
2019-10-15 21:33:43 +02:00
|
|
|
mem = meminfo(ptr);
|
2017-08-30 11:47:26 +02:00
|
|
|
if (mem->flags & KORE_MEM_TAGGED) {
|
|
|
|
kore_mem_untag(ptr);
|
|
|
|
mem->flags &= ~KORE_MEM_TAGGED;
|
|
|
|
}
|
|
|
|
|
2019-10-15 21:33:43 +02:00
|
|
|
size = memsize(ptr);
|
|
|
|
addr = (u_int8_t *)ptr - sizeof(struct memsize);
|
2013-06-27 08:43:07 +02:00
|
|
|
|
2019-10-15 21:33:43 +02:00
|
|
|
if (size->len <= KORE_MEM_BLOCK_SIZE_MAX) {
|
|
|
|
idx = memblock_index(size->len);
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_pool_put(&blocks[idx].pool, addr);
|
|
|
|
} else {
|
|
|
|
free(addr);
|
|
|
|
}
|
2013-06-27 08:43:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
kore_strdup(const char *str)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *nstr;
|
|
|
|
|
|
|
|
len = strlen(str) + 1;
|
2013-07-13 21:08:55 +02:00
|
|
|
nstr = kore_malloc(len);
|
2016-07-04 11:41:37 +02:00
|
|
|
(void)kore_strlcpy(nstr, str, len);
|
2013-06-27 08:43:07 +02:00
|
|
|
|
|
|
|
return (nstr);
|
|
|
|
}
|
2016-07-12 13:54:14 +02:00
|
|
|
|
2017-08-30 11:47:26 +02:00
|
|
|
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);
|
|
|
|
|
2019-10-15 21:33:43 +02:00
|
|
|
mem = meminfo(ptr);
|
2017-08-30 11:47:26 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-12 13:54:14 +02:00
|
|
|
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);
|
|
|
|
}
|
2019-10-15 21:33:43 +02:00
|
|
|
|
|
|
|
static inline struct memsize *
|
|
|
|
memsize(void *ptr)
|
|
|
|
{
|
|
|
|
struct memsize *ms;
|
|
|
|
|
|
|
|
ms = (struct memsize *)((u_int8_t *)ptr - sizeof(*ms));
|
|
|
|
|
|
|
|
if (ms->magic != KORE_MEM_MAGIC)
|
|
|
|
fatal("%s: bad memsize magic (0x%x)", __func__, ms->magic);
|
|
|
|
|
|
|
|
return (ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct meminfo *
|
|
|
|
meminfo(void *ptr)
|
|
|
|
{
|
|
|
|
struct memsize *ms;
|
|
|
|
struct meminfo *info;
|
|
|
|
|
|
|
|
ms = memsize(ptr);
|
|
|
|
info = (struct meminfo *)((u_int8_t *)ptr + ms->len);
|
|
|
|
|
|
|
|
if (info->magic != KORE_MEM_MAGIC)
|
|
|
|
fatal("%s: bad meminfo magic (0x%x)", __func__, info->magic);
|
|
|
|
|
|
|
|
return (info);
|
|
|
|
}
|