From 158b2b3ba66271f0087da13585cbf80913c66eb7 Mon Sep 17 00:00:00 2001
From: "Meredith L. Patterson" <mlp@thesmartpolitenerd.com>
Date: Wed, 10 Oct 2012 16:24:12 +0200
Subject: [PATCH] Removed all glib functions from everything other than the
 test suite.

---
 src/bitreader.c        |  2 ++
 src/bitwriter.c        |  4 ++++
 src/hammer.c           | 12 +++++++-----
 src/hammer.h           |  1 -
 src/internal.h         |  1 -
 src/parsers/ch.c       |  4 ++--
 src/parsers/choice.c   |  1 +
 src/parsers/sequence.c |  1 +
 src/pprint.c           | 15 ++++++++++-----
 src/test_suite.c       |  1 +
 10 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/src/bitreader.c b/src/bitreader.c
index bb93377b..b0018f2d 100644
--- a/src/bitreader.c
+++ b/src/bitreader.c
@@ -111,6 +111,8 @@ long long h_read_bits(HInputStream* state, int count, char signed_p) {
 
 #ifdef INCLUDE_TESTS
 
+#include <glib.h>
+
 #define MK_INPUT_STREAM(buf,len,endianness_)   \
   {					      \
     .input = (uint8_t*)buf,					\
diff --git a/src/bitwriter.c b/src/bitwriter.c
index e716f094..956f2ea9 100644
--- a/src/bitwriter.c
+++ b/src/bitwriter.c
@@ -4,6 +4,9 @@
 #include "internal.h"
 #include "test_suite.h"
 
+#define MIN(a,b) (((a)<(b))?(a):(b))
+#define MAX(a,b) (((a)>(b))?(a):(b))
+
 // This file provides the logical inverse of bitreader.c
 struct HBitWriter_ {
   uint8_t* buf;
@@ -109,6 +112,7 @@ void h_bit_writer_free(HBitWriter* w) {
 }
 
 #ifdef INCLUDE_TESTS
+#include <glib.h>
 // TESTS BELOW HERE
 typedef struct {
   unsigned long long data;
diff --git a/src/hammer.c b/src/hammer.c
index 52881ec6..d3782681 100644
--- a/src/hammer.c
+++ b/src/hammer.c
@@ -26,8 +26,8 @@
 #include "allocator.h"
 #include "parsers/parser_internal.h"
 
-static guint djbhash(const uint8_t *buf, size_t len) {
-  guint hash = 5381;
+static uint32_t djbhash(const uint8_t *buf, size_t len) {
+  uint32_t hash = 5381;
   while (len--) {
     hash = hash * 33 + *buf++;
   }
@@ -116,7 +116,7 @@ void setupLR(const HParser *p, HParseState *state, HLeftRec *rec_detect) {
   HLeftRec *lr = state->lr_stack->head->elem;
   while (lr && lr->rule != p) {
     lr->head = rec_detect->head;
-    h_slist_push(lr->head->involved_set, (gpointer)lr->rule);
+    h_slist_push(lr->head->involved_set, (void*)lr->rule);
   }
 }
 
@@ -230,10 +230,10 @@ typedef struct {
 } HTwoParsers;
 
 
-static guint cache_key_hash(gconstpointer key) {
+static uint32_t cache_key_hash(const void* key) {
   return djbhash(key, sizeof(HParserCacheKey));
 }
-static gboolean cache_key_equal(gconstpointer key1, gconstpointer key2) {
+static bool cache_key_equal(const void* key1, const void* key2) {
   return memcmp(key1, key2, sizeof(HParserCacheKey)) == 0;
 }
 
@@ -274,7 +274,9 @@ void h_parse_result_free(HParseResult *result) {
 
 #ifdef INCLUDE_TESTS
 
+#include <glib.h>
 #include "test_suite.h"
+
 static void test_token(void) {
   const HParser *token_ = h_token((const uint8_t*)"95\xa2", 3);
 
diff --git a/src/hammer.h b/src/hammer.h
index 2c5a5cf2..1c1a6cee 100644
--- a/src/hammer.h
+++ b/src/hammer.h
@@ -17,7 +17,6 @@
 
 #ifndef HAMMER_HAMMER__H
 #define HAMMER_HAMMER__H
-#include <glib.h>
 #include <stdint.h>
 #include <stdio.h>
 #include "allocator.h"
diff --git a/src/internal.h b/src/internal.h
index b7f56b60..a24cc0e7 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -17,7 +17,6 @@
 
 #ifndef HAMMER_INTERNAL__H
 #define HAMMER_INTERNAL__H
-#include <glib.h>
 #include <err.h>
 #include "hammer.h"
 
diff --git a/src/parsers/ch.c b/src/parsers/ch.c
index bb3073f0..032731e9 100644
--- a/src/parsers/ch.c
+++ b/src/parsers/ch.c
@@ -1,7 +1,7 @@
 #include "parser_internal.h"
 
 static HParseResult* parse_ch(void* env, HParseState *state) {
-  uint8_t c = (uint8_t)GPOINTER_TO_UINT(env);
+  uint8_t c = (uint8_t)(unsigned long)(env);
   uint8_t r = (uint8_t)h_read_bits(&state->input_stream, 8, false);
   if (c == r) {
     HParsedToken *tok = a_new(HParsedToken, 1);    
@@ -22,6 +22,6 @@ const HParser* h_ch(const uint8_t c) {
 const HParser* h_ch__m(HAllocator* mm__, const uint8_t c) {  
   HParser *ret = h_new(HParser, 1);
   ret->vtable = &ch_vt;
-  ret->env = GUINT_TO_POINTER(c);
+  ret->env = (void*)(unsigned long)(c);
   return (const HParser*)ret;
 }
diff --git a/src/parsers/choice.c b/src/parsers/choice.c
index 24303522..73dedde6 100644
--- a/src/parsers/choice.c
+++ b/src/parsers/choice.c
@@ -1,3 +1,4 @@
+#include <stdarg.h>
 #include "parser_internal.h"
 
 typedef struct {
diff --git a/src/parsers/sequence.c b/src/parsers/sequence.c
index dece4f0e..21ae31d9 100644
--- a/src/parsers/sequence.c
+++ b/src/parsers/sequence.c
@@ -1,3 +1,4 @@
+#include <stdarg.h>
 #include "parser_internal.h"
 
 typedef struct {
diff --git a/src/pprint.c b/src/pprint.c
index 8dc58524..3a8df824 100644
--- a/src/pprint.c
+++ b/src/pprint.c
@@ -17,9 +17,9 @@
 
 #define _GNU_SOURCE
 #include <stdio.h>
-#include <glib.h>
 #include <string.h>
 #include "hammer.h"
+#include "internal.h"
 #include <malloc.h>
 
 typedef struct pp_state {
@@ -69,20 +69,21 @@ void h_pprint(FILE* stream, const HParsedToken* tok, int indent, int delta) {
     fprintf(stream, "%*sUSER\n", indent, "");
     break;
   default:
-    g_assert_not_reached();
+    assert_message(0, "Should not reach here.");
   }
 }
 
 
 struct result_buf {
   char* output;
+  HAllocator *mm__;
   size_t len;
   size_t capacity;
 };
 
 static inline void ensure_capacity(struct result_buf *buf, int amt) {
   while (buf->len + amt >= buf->capacity)
-    buf->output = g_realloc(buf->output, buf->capacity *= 2);
+    buf->output = buf->mm__->realloc(buf->mm__, buf->output, buf->capacity *= 2);
 }
 
 static inline void append_buf(struct result_buf *buf, const char* input, int len) {
@@ -149,15 +150,19 @@ static void unamb_sub(const HParsedToken* tok, struct result_buf *buf) {
     break;
   default:
     fprintf(stderr, "Unexpected token type %d\n", tok->token_type);
-    g_assert_not_reached();
+    assert_message(0, "Should not reach here.");
   }
 }
   
 
 char* h_write_result_unamb(const HParsedToken* tok) {
+  return h_write_result_unamb__m(&system_allocator, tok);
+}
+char* h_write_result_unamb__m(HAllocator* mm__, const HParsedToken* tok) {
   struct result_buf buf = {
-    .output = g_malloc0(16),
+    .output = mm__->alloc(mm__, 16),
     .len = 0,
+    .mm__ = mm__,
     .capacity = 16
   };
   unamb_sub(tok, &buf);
diff --git a/src/test_suite.c b/src/test_suite.c
index af22c7e9..16b3ef7c 100644
--- a/src/test_suite.c
+++ b/src/test_suite.c
@@ -15,6 +15,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 
+#include <glib.h>
 #include "hammer.h"
 #include "test_suite.h"
 
-- 
GitLab