2016-11-20 00:38:15 +00:00
|
|
|
/*
|
2018-12-31 03:35:00 +00:00
|
|
|
** © 2009-2017 by Kornel Lesiński.
|
|
|
|
** © 1989, 1991 by Jef Poskanzer.
|
|
|
|
** © 1997, 2000, 2002 by Greg Roelofs; based on an idea by Stefan Schneider.
|
|
|
|
**
|
|
|
|
** See COPYRIGHT file for license.
|
2016-11-20 00:38:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libimagequant.h"
|
|
|
|
#include "mempool.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#define ALIGN_MASK 15UL
|
|
|
|
#define MEMPOOL_RESERVED ((sizeof(struct mempool)+ALIGN_MASK) & ~ALIGN_MASK)
|
|
|
|
|
|
|
|
struct mempool {
|
|
|
|
unsigned int used, size;
|
|
|
|
void* (*malloc)(size_t);
|
|
|
|
void (*free)(void*);
|
|
|
|
struct mempool *next;
|
|
|
|
};
|
2018-12-31 03:35:00 +00:00
|
|
|
LIQ_PRIVATE void* mempool_create(mempoolptr *mptr, const unsigned int size, unsigned int max_size, void* (*malloc)(size_t), void (*free)(void*))
|
2016-11-20 00:38:15 +00:00
|
|
|
{
|
|
|
|
if (*mptr && ((*mptr)->used+size) <= (*mptr)->size) {
|
|
|
|
unsigned int prevused = (*mptr)->used;
|
|
|
|
(*mptr)->used += (size+15UL) & ~0xFUL;
|
|
|
|
return ((char*)(*mptr)) + prevused;
|
|
|
|
}
|
|
|
|
|
2018-12-31 03:35:00 +00:00
|
|
|
mempoolptr old = *mptr;
|
2016-11-20 00:38:15 +00:00
|
|
|
if (!max_size) max_size = (1<<17);
|
|
|
|
max_size = size+ALIGN_MASK > max_size ? size+ALIGN_MASK : max_size;
|
|
|
|
|
|
|
|
*mptr = malloc(MEMPOOL_RESERVED + max_size);
|
|
|
|
if (!*mptr) return NULL;
|
|
|
|
**mptr = (struct mempool){
|
|
|
|
.malloc = malloc,
|
|
|
|
.free = free,
|
|
|
|
.size = MEMPOOL_RESERVED + max_size,
|
|
|
|
.used = sizeof(struct mempool),
|
|
|
|
.next = old,
|
|
|
|
};
|
|
|
|
uintptr_t mptr_used_start = (uintptr_t)(*mptr) + (*mptr)->used;
|
|
|
|
(*mptr)->used += (ALIGN_MASK + 1 - (mptr_used_start & ALIGN_MASK)) & ALIGN_MASK; // reserve bytes required to make subsequent allocations aligned
|
|
|
|
assert(!(((uintptr_t)(*mptr) + (*mptr)->used) & ALIGN_MASK));
|
|
|
|
|
|
|
|
return mempool_alloc(mptr, size, size);
|
|
|
|
}
|
|
|
|
|
2018-12-31 03:35:00 +00:00
|
|
|
LIQ_PRIVATE void* mempool_alloc(mempoolptr *mptr, const unsigned int size, const unsigned int max_size)
|
2016-11-20 00:38:15 +00:00
|
|
|
{
|
|
|
|
if (((*mptr)->used+size) <= (*mptr)->size) {
|
|
|
|
unsigned int prevused = (*mptr)->used;
|
|
|
|
(*mptr)->used += (size + ALIGN_MASK) & ~ALIGN_MASK;
|
|
|
|
return ((char*)(*mptr)) + prevused;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mempool_create(mptr, size, max_size, (*mptr)->malloc, (*mptr)->free);
|
|
|
|
}
|
|
|
|
|
2018-12-31 03:35:00 +00:00
|
|
|
LIQ_PRIVATE void mempool_destroy(mempoolptr m)
|
2016-11-20 00:38:15 +00:00
|
|
|
{
|
|
|
|
while (m) {
|
2018-12-31 03:35:00 +00:00
|
|
|
mempoolptr next = m->next;
|
2016-11-20 00:38:15 +00:00
|
|
|
m->free(m);
|
|
|
|
m = next;
|
|
|
|
}
|
|
|
|
}
|