Skip to content

Commit

Permalink
Simplified allocator to allocate only with alignment 8
Browse files Browse the repository at this point in the history
  • Loading branch information
vivkin committed May 6, 2014
1 parent 3822317 commit 869e8e6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
5 changes: 1 addition & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ project(gason)

option(SHOOTOUT "Build parser-shootout" OFF)

if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-stdlib=libc++)
endif()
add_compile_options(-std=c++11 -fno-rtti -fno-exceptions -Wall -Wextra)
add_compile_options(-O3 -std=c++11 -fno-rtti -fno-exceptions -Wall -Wextra)

add_executable(gason-pp gason.cpp pretty-print.cpp)
add_executable(gason-test-suite gason.cpp test-suite.cpp)
Expand Down
41 changes: 20 additions & 21 deletions gason.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@
#define JSON_STACK_SIZE 32

JsonAllocator::~JsonAllocator() {
while (head) {
Zone *next = head->next;
free(head);
head = next;
}
}

static inline void *alignPointer(void *p, size_t alignment) {
return (void *)(((uintptr_t)p + (alignment - 1)) & ~(alignment - 1));
deallocate();
}

void *JsonAllocator::allocate(size_t size, size_t alignment) {
void *JsonAllocator::allocate(size_t size) {
assert(size & 7 == 0);
if (head) {
char *p = (char *)alignPointer(head->end, alignment);
if (p + size <= (char *)head + JSON_ZONE_SIZE) {
head->end = p + size;
if (head->used + size <= JSON_ZONE_SIZE) {
char *p = (char *)head + head->used;
head->used += size;
return p;
}
}
size_t zoneSize = sizeof(Zone) + (size + (alignment - 1) & ~(alignment - 1));
Zone *z = (Zone *)malloc(zoneSize <= JSON_ZONE_SIZE ? JSON_ZONE_SIZE : zoneSize);
char *p = (char *)alignPointer(z + 1, alignment);
z->end = p + size;
if (zoneSize <= JSON_ZONE_SIZE || head == nullptr) {
size_t allocSize = sizeof(Zone) + size;
Zone *z = (Zone *)malloc(allocSize <= JSON_ZONE_SIZE ? JSON_ZONE_SIZE : allocSize);
z->used = allocSize;
if (allocSize <= JSON_ZONE_SIZE || head == nullptr) {
z->next = head;
head = z;
} else {
z->next = head->next;
head->next = z;
}
return p;
return (char *)z + sizeof(Zone);
}

void JsonAllocator::deallocate() {
while (head) {
Zone *next = head->next;
free(head);
head = next;
}
}

static inline bool isdelim(char c) {
Expand Down Expand Up @@ -297,13 +297,12 @@ JsonParseStatus gasonParse(char *s, char **endptr, JsonValue *value, JsonAllocat
continue;
}
tails[top] = insertAfter(tails[top], (JsonNode *)allocator.allocate(sizeof(JsonNode)));
tails[top]->value = o;
tails[top]->key = keys[top];
keys[top] = nullptr;
} else {
tails[top] = insertAfter(tails[top], (JsonNode *)allocator.allocate(sizeof(JsonNode) - sizeof(char *)));
tails[top]->value = o;
}
tails[top]->value = o;
}
return JSON_PARSE_BREAKING_BAD;
}
24 changes: 12 additions & 12 deletions gason.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
#include <stdint.h>
#include <assert.h>

struct JsonAllocator {
struct Zone {
Zone *next;
char *end;
};

Zone *head = nullptr;

~JsonAllocator();
void *allocate(size_t size, size_t alignment = 8);
};

#define JSON_VALUE_PAYLOAD_MASK 0x00007FFFFFFFFFFFULL
#define JSON_VALUE_NAN_MASK 0x7FF8000000000000ULL
#define JSON_VALUE_NULL 0x7FFF800000000000ULL
Expand Down Expand Up @@ -122,4 +110,16 @@ enum JsonParseStatus {
JSON_PARSE_BREAKING_BAD
};

class JsonAllocator {

This comment has been minimized.

Copy link
@rmartinho

rmartinho Jul 23, 2014

I'm revoking your C++ license. This class violates the rule of three.

struct Zone {
Zone *next;
size_t used;
} *head = nullptr;

public:
~JsonAllocator();
void *allocate(size_t size);
void deallocate();
};

JsonParseStatus gasonParse(char *str, char **endptr, JsonValue *value, JsonAllocator &allocator);

0 comments on commit 869e8e6

Please sign in to comment.