issue #128 refactor membuff_t to move it from the debugger to a shared location so that we can reuse it in the JIT implementation, some more initial work on the new backend
parent
b2ddc8038e
commit
1443fe9099
@ -1,4 +1,4 @@
|
||||
mkdir nojit64a
|
||||
cd nojit64a
|
||||
cmake -DCMAKE_INSTALL_PREFIX=c:\ravi -DCMAKE_BUILD_TYPE=Debug -G "Visual Studio 14 Win64" ..
|
||||
cmake -DCMAKE_INSTALL_PREFIX=c:\ravi -DCMAKE_BUILD_TYPE=Debug -G "Visual Studio 15 Win64" ..
|
||||
cd ..
|
@ -1,4 +1,4 @@
|
||||
mkdir nojit64
|
||||
cd nojit64
|
||||
cmake -DCMAKE_INSTALL_PREFIX=c:\ravi -DCMAKE_BUILD_TYPE=Release -DSTATIC_BUILD=ON -G "Visual Studio 14 Win64" ..
|
||||
cmake -DCMAKE_INSTALL_PREFIX=c:\ravi -DCMAKE_BUILD_TYPE=Release -DSTATIC_BUILD=ON -G "Visual Studio 15 Win64" ..
|
||||
cd ..
|
@ -0,0 +1,27 @@
|
||||
#ifndef RAVI_MEMBUF_H
|
||||
#define RAVI_MEMBUF_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
char *buf;
|
||||
size_t allocated_size;
|
||||
size_t pos;
|
||||
} membuff_t;
|
||||
|
||||
extern void membuff_init(membuff_t *mb, size_t initial_size);
|
||||
extern void membuff_rewindpos(membuff_t *mb);
|
||||
extern void membuff_resize(membuff_t *mb, size_t new_size);
|
||||
extern void membuff_free(membuff_t *mb);
|
||||
extern void membuff_add_string(membuff_t *mb, const char *str);
|
||||
extern void membuff_add_bool(membuff_t *mb, bool value);
|
||||
extern void membuff_add_int(membuff_t *mb, int value);
|
||||
extern void membuff_add_longlong(membuff_t *mb, int64_t value);
|
||||
|
||||
/* strncpy() with guaranteed 0 termination */
|
||||
extern void ravi_string_copy(char *buf, const char *src, size_t buflen);
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,65 @@
|
||||
#include <ravi_membuf.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void ravi_string_copy(char *buf, const char *src, size_t buflen) {
|
||||
if (buflen == 0) return;
|
||||
strncpy(buf, src, buflen);
|
||||
buf[buflen - 1] = 0;
|
||||
}
|
||||
|
||||
void membuff_init(membuff_t *mb, size_t initial_size) {
|
||||
if (initial_size > 0) {
|
||||
mb->buf = (char *)calloc(1, initial_size);
|
||||
if (mb->buf == NULL) {
|
||||
fprintf(stderr, "out of memory\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else
|
||||
mb->buf = NULL;
|
||||
mb->pos = 0;
|
||||
mb->allocated_size = initial_size;
|
||||
}
|
||||
void membuff_rewindpos(membuff_t *mb) { mb->pos = 0; }
|
||||
void membuff_resize(membuff_t *mb, size_t new_size) {
|
||||
if (new_size <= mb->allocated_size) return;
|
||||
char *newmem = (char *)realloc(mb->buf, new_size);
|
||||
if (newmem == NULL) {
|
||||
fprintf(stderr, "out of memory\n");
|
||||
exit(1);
|
||||
}
|
||||
mb->buf = newmem;
|
||||
mb->allocated_size = new_size;
|
||||
}
|
||||
void membuff_free(membuff_t *mb) { free(mb->buf); }
|
||||
void membuff_add_string(membuff_t *mb, const char *str) {
|
||||
size_t len = strlen(str);
|
||||
size_t required_size = mb->pos + len + 1; /* extra byte for NULL terminator */
|
||||
membuff_resize(mb, required_size);
|
||||
assert(mb->allocated_size - mb->pos > len);
|
||||
ravi_string_copy(&mb->buf[mb->pos], str, mb->allocated_size - mb->pos);
|
||||
mb->pos += len;
|
||||
}
|
||||
void membuff_add_bool(membuff_t *mb, bool value) {
|
||||
if (value)
|
||||
membuff_add_string(mb, "true");
|
||||
else
|
||||
membuff_add_string(mb, "false");
|
||||
}
|
||||
void membuff_add_int(membuff_t *mb, int value) {
|
||||
char temp[100];
|
||||
snprintf(temp, sizeof temp, "%d", value);
|
||||
membuff_add_string(mb, temp);
|
||||
}
|
||||
void membuff_add_longlong(membuff_t *mb, int64_t value) {
|
||||
char temp[100];
|
||||
snprintf(temp, sizeof temp, "%" PRId64 "", value);
|
||||
membuff_add_string(mb, temp);
|
||||
}
|
Loading…
Reference in new issue