2011-08-16 21:48:47 -05:00
|
|
|
// Object stacks, used in lieu of dynamically-sized frames.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "rust_internal.h"
|
|
|
|
#include "rust_obstack.h"
|
|
|
|
#include "rust_task.h"
|
|
|
|
|
|
|
|
// ISAAC, let go of max()!
|
|
|
|
#ifdef max
|
|
|
|
#undef max
|
|
|
|
#endif
|
|
|
|
|
2011-08-17 19:20:36 -05:00
|
|
|
//#define DPRINT(fmt,...) fprintf(stderr, fmt, ##__VA_ARGS__)
|
|
|
|
#define DPRINT(fmt,...)
|
|
|
|
|
|
|
|
//const size_t DEFAULT_CHUNK_SIZE = 4096;
|
|
|
|
const size_t DEFAULT_CHUNK_SIZE = 300000;
|
|
|
|
const size_t DEFAULT_ALIGNMENT = 16;
|
2011-08-16 21:48:47 -05:00
|
|
|
|
|
|
|
struct rust_obstack_chunk {
|
|
|
|
rust_obstack_chunk *prev;
|
|
|
|
size_t size;
|
|
|
|
size_t alen;
|
|
|
|
size_t pad;
|
|
|
|
uint8_t data[];
|
|
|
|
|
|
|
|
rust_obstack_chunk(rust_obstack_chunk *in_prev, size_t in_size)
|
|
|
|
: prev(in_prev), size(in_size), alen(0) {}
|
|
|
|
|
|
|
|
void *alloc(size_t len);
|
|
|
|
bool free(void *ptr);
|
|
|
|
};
|
|
|
|
|
|
|
|
void *
|
|
|
|
rust_obstack_chunk::alloc(size_t len) {
|
2011-08-17 19:20:36 -05:00
|
|
|
alen = align_to(alen, DEFAULT_ALIGNMENT);
|
|
|
|
|
|
|
|
if (len > size - alen) {
|
|
|
|
DPRINT("Not enough space, len=%lu!\n", len);
|
|
|
|
assert(0);
|
2011-08-16 21:48:47 -05:00
|
|
|
return NULL; // Not enough space.
|
2011-08-17 19:20:36 -05:00
|
|
|
}
|
2011-08-16 21:48:47 -05:00
|
|
|
void *result = data + alen;
|
|
|
|
alen += len;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
rust_obstack_chunk::free(void *ptr) {
|
|
|
|
uint8_t *p = (uint8_t *)ptr;
|
2011-08-17 19:20:36 -05:00
|
|
|
if (p < data || p > data + size)
|
2011-08-16 21:48:47 -05:00
|
|
|
return false;
|
2011-08-17 15:58:49 -05:00
|
|
|
assert(p <= data + alen);
|
2011-08-16 21:48:47 -05:00
|
|
|
alen = (size_t)(p - data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocates the given number of bytes in a new chunk.
|
|
|
|
void *
|
|
|
|
rust_obstack::alloc_new(size_t len) {
|
|
|
|
size_t chunk_size = std::max(len, DEFAULT_CHUNK_SIZE);
|
|
|
|
void *ptr = task->malloc(sizeof(chunk) + chunk_size, "obstack");
|
2011-08-17 19:20:36 -05:00
|
|
|
DPRINT("making new chunk at %p, len %lu\n", ptr, chunk_size);
|
2011-08-16 21:48:47 -05:00
|
|
|
chunk = new(ptr) rust_obstack_chunk(chunk, chunk_size);
|
|
|
|
return chunk->alloc(len);
|
|
|
|
}
|
|
|
|
|
2011-08-17 15:15:04 -05:00
|
|
|
rust_obstack::~rust_obstack() {
|
|
|
|
while (chunk) {
|
|
|
|
rust_obstack_chunk *prev = chunk->prev;
|
|
|
|
task->free(chunk);
|
|
|
|
chunk = prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-16 21:48:47 -05:00
|
|
|
void *
|
|
|
|
rust_obstack::alloc(size_t len) {
|
|
|
|
if (!chunk)
|
|
|
|
return alloc_new(len);
|
2011-08-17 19:20:36 -05:00
|
|
|
|
|
|
|
DPRINT("alloc sz %u", (uint32_t)len);
|
|
|
|
|
2011-08-16 21:48:47 -05:00
|
|
|
void *ptr = chunk->alloc(len);
|
2011-08-17 15:58:49 -05:00
|
|
|
ptr = ptr ? ptr : alloc_new(len);
|
2011-08-17 19:20:36 -05:00
|
|
|
|
2011-08-17 15:58:49 -05:00
|
|
|
return ptr;
|
2011-08-16 21:48:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_obstack::free(void *ptr) {
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
|
2011-08-17 19:20:36 -05:00
|
|
|
DPRINT("free ptr %p\n", ptr);
|
|
|
|
|
2011-08-16 21:48:47 -05:00
|
|
|
assert(chunk);
|
|
|
|
while (!chunk->free(ptr)) {
|
2011-08-17 19:20:36 -05:00
|
|
|
DPRINT("deleting chunk at %p\n", chunk);
|
2011-08-16 21:48:47 -05:00
|
|
|
rust_obstack_chunk *prev = chunk->prev;
|
|
|
|
task->free(chunk);
|
|
|
|
chunk = prev;
|
|
|
|
assert(chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|