#include "context.h"
#include "macros.h"
#include "slang_mem.h"
#define GRANULARITY 8
#define ROUND_UP(B) ( ((B) + (GRANULARITY - 1)) & ~(GRANULARITY - 1) )
#define USE_MALLOC_FREE 0
struct slang_mempool_
{
GLuint Size, Used, Count, Largest;
char *Data;
struct slang_mempool_ *Next;
};
slang_mempool *
_slang_new_mempool(GLuint initialSize)
{
slang_mempool *pool = (slang_mempool *) _mesa_calloc(sizeof(slang_mempool));
if (pool) {
pool->Data = (char *) _mesa_calloc(initialSize);
if (!pool->Data) {
_mesa_free(pool);
return NULL;
}
pool->Size = initialSize;
pool->Used = 0;
}
return pool;
}
void
_slang_delete_mempool(slang_mempool *pool)
{
GLuint total = 0;
while (pool) {
slang_mempool *next = pool->Next;
total += pool->Used;
_mesa_free(pool->Data);
_mesa_free(pool);
pool = next;
}
}
#ifdef DEBUG
static void
check_zero(const char *addr, GLuint n)
{
GLuint i;
for (i = 0; i < n; i++) {
assert(addr[i]==0);
}
}
#endif
#ifdef DEBUG
static GLboolean
is_valid_address(const slang_mempool *pool, void *addr)
{
while (pool) {
if ((char *) addr >= pool->Data &&
(char *) addr < pool->Data + pool->Used)
return GL_TRUE;
pool = pool->Next;
}
return GL_FALSE;
}
#endif
void *
_slang_alloc(GLuint bytes)
{
#if USE_MALLOC_FREE
return _mesa_calloc(bytes);
#else
slang_mempool *pool;
GET_CURRENT_CONTEXT(ctx);
pool = (slang_mempool *) ctx->Shader.MemPool;
if (bytes == 0)
bytes = 1;
while (pool) {
if (pool->Used + bytes <= pool->Size) {
void *addr = (void *) (pool->Data + pool->Used);
#ifdef DEBUG
check_zero((char*) addr, bytes);
#endif
pool->Used += ROUND_UP(bytes);
pool->Largest = MAX2(pool->Largest, bytes);
pool->Count++;
return addr;
}
else if (pool->Next) {
pool = pool->Next;
}
else {
const GLuint sz = MAX2(bytes, pool->Size);
pool->Next = _slang_new_mempool(sz);
if (!pool->Next) {
return NULL;
}
else {
pool = pool->Next;
pool->Largest = bytes;
pool->Count++;
pool->Used = ROUND_UP(bytes);
#ifdef DEBUG
check_zero((char*) pool->Data, bytes);
#endif
return (void *) pool->Data;
}
}
}
return NULL;
#endif
}
void *
_slang_realloc(void *oldBuffer, GLuint oldSize, GLuint newSize)
{
#if USE_MALLOC_FREE
return _mesa_realloc(oldBuffer, oldSize, newSize);
#else
GET_CURRENT_CONTEXT(ctx);
slang_mempool *pool = (slang_mempool *) ctx->Shader.MemPool;
if (newSize < oldSize) {
return oldBuffer;
}
else {
const GLuint copySize = (oldSize < newSize) ? oldSize : newSize;
void *newBuffer = _slang_alloc(newSize);
if (oldBuffer)
ASSERT(is_valid_address(pool, oldBuffer));
if (newBuffer && oldBuffer && copySize > 0)
_mesa_memcpy(newBuffer, oldBuffer, copySize);
return newBuffer;
}
#endif
}
char *
_slang_strdup(const char *s)
{
if (s) {
size_t l = _mesa_strlen(s);
char *s2 = (char *) _slang_alloc(l + 1);
if (s2)
_mesa_strcpy(s2, s);
return s2;
}
else {
return NULL;
}
}
void
_slang_free(void *addr)
{
#if USE_MALLOC_FREE
_mesa_free(addr);
#else
if (addr) {
GET_CURRENT_CONTEXT(ctx);
slang_mempool *pool = (slang_mempool *) ctx->Shader.MemPool;
ASSERT(is_valid_address(pool, addr));
}
#endif
}