Compare commits
No commits in common. "master" and "lmdbwerk" have entirely different histories.
20 changed files with 213 additions and 733 deletions
|
@ -1,18 +0,0 @@
|
||||||
name: Compile Cytoplasm
|
|
||||||
run-name: Compile Cytoplasm on ${{ forgejo.actor }}
|
|
||||||
on: [push, pull_request]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
"Compile Cytoplasm":
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os: [alpine]
|
|
||||||
arch: [aarch64]
|
|
||||||
runs-on: ["${{ matrix.os }}", "${{ matrix.arch }}"]
|
|
||||||
steps:
|
|
||||||
- name: Check out repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
- name: Configure Cytoplasm
|
|
||||||
run: ./configure
|
|
||||||
- name: Build Cytoplasm
|
|
||||||
run: make
|
|
25
.gitea/workflows/compile.yaml
Normal file
25
.gitea/workflows/compile.yaml
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
name: Compile Cytoplasm
|
||||||
|
run-name: Compile Cytoplasm on ${{ gitea.actor }}
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
"Compile Cytoplasm":
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [debian-v12.4, alpine-v3.19, openbsd-v7.4, freebsd-v14.0, netbsd-v9.3]
|
||||||
|
arch: [x86, x86_64]
|
||||||
|
exclude:
|
||||||
|
# 32-bit OpenBSD does not behave well in QEMU. Even when using
|
||||||
|
# QEMU to emulate i386, it utilizes 100% of its CPU core and is
|
||||||
|
# still extremely sluggish. Thus, we don't have a working 32-bit
|
||||||
|
# OpenBSD runner, so exclude it from the matrix configuration.
|
||||||
|
- os: openbsd-v7.4
|
||||||
|
arch: x86
|
||||||
|
runs-on: ["${{ matrix.os }}", "${{ matrix.arch }}"]
|
||||||
|
steps:
|
||||||
|
- name: Check out repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Configure Cytoplasm
|
||||||
|
run: ./configure
|
||||||
|
- name: Build Cytoplasm
|
||||||
|
run: make
|
|
@ -21,8 +21,6 @@ are not even initialized to a default value.
|
||||||
- Make `HttpRouter` decode path parts before matching them on regular expressions.
|
- Make `HttpRouter` decode path parts before matching them on regular expressions.
|
||||||
- Fixed a bug in `ArraySort()` that would crash programs if the passed array has no
|
- Fixed a bug in `ArraySort()` that would crash programs if the passed array has no
|
||||||
elements.
|
elements.
|
||||||
- Adds `Db[OP]Args` functions that are equivalent to their `Db[OP]` counter parts, but
|
|
||||||
uses an array of string instead of variadic arguments.
|
|
||||||
|
|
||||||
## v0.4.0
|
## v0.4.0
|
||||||
|
|
||||||
|
|
8
configure
vendored
8
configure
vendored
|
@ -37,7 +37,7 @@ case "$(uname)" in
|
||||||
# These systems typically use GCC.
|
# These systems typically use GCC.
|
||||||
SCRIPT_ARGS="${SCRIPT_ARGS} --cc=gcc"
|
SCRIPT_ARGS="${SCRIPT_ARGS} --cc=gcc"
|
||||||
;;
|
;;
|
||||||
OpenBSD|FreeBSD|Darwin)
|
OpenBSD|FreeBSD)
|
||||||
# These systems typically use Clang.
|
# These systems typically use Clang.
|
||||||
SCRIPT_ARGS="${SCRIPT_ARGS} --cc=clang"
|
SCRIPT_ARGS="${SCRIPT_ARGS} --cc=clang"
|
||||||
;;
|
;;
|
||||||
|
@ -269,10 +269,8 @@ ${TAB}done
|
||||||
${LIB_NAME}: ${OUT}/lib/lib${LIB_NAME}.a ${OUT}/lib/lib${LIB_NAME}.so
|
${LIB_NAME}: ${OUT}/lib/lib${LIB_NAME}.a ${OUT}/lib/lib${LIB_NAME}.so
|
||||||
|
|
||||||
install: ${LIB_NAME}
|
install: ${LIB_NAME}
|
||||||
${TAB}mkdir -p \$(PREFIX)/${OUT}/lib
|
${TAB}install -D ${OUT}/lib/lib${LIB_NAME}.a \$(PREFIX)/lib/lib${LIB_NAME}.a
|
||||||
${TAB}mkdir -p \$(PREFIX)/lib
|
${TAB}install -D ${OUT}/lib/lib${LIB_NAME}.so \$(PREFIX)/lib/lib${LIB_NAME}.so
|
||||||
${TAB}cp ${OUT}/lib/lib${LIB_NAME}.a \$(PREFIX)/lib/lib${LIB_NAME}.a
|
|
||||||
${TAB}cp ${OUT}/lib/lib${LIB_NAME}.so \$(PREFIX)/lib/lib${LIB_NAME}.so
|
|
||||||
$(collect ${INCLUDE}/ '' '' \$\(PREFIX\)/include/${LIB_NAME}/ install_out)
|
$(collect ${INCLUDE}/ '' '' \$\(PREFIX\)/include/${LIB_NAME}/ install_out)
|
||||||
$(collect ${INCLUDE}/ .h .3 \$\(PREFIX\)/man/man3/${LIB_NAME}- install_man)
|
$(collect ${INCLUDE}/ .h .3 \$\(PREFIX\)/man/man3/${LIB_NAME}- install_man)
|
||||||
$(collect ${TOOLS}/ '.c' '' \$\(PREFIX\)/bin/ install_tool)
|
$(collect ${TOOLS}/ '.c' '' \$\(PREFIX\)/bin/ install_tool)
|
||||||
|
|
|
@ -119,13 +119,6 @@ extern void DbMaxCacheSet(Db *, size_t);
|
||||||
*/
|
*/
|
||||||
extern DbRef * DbCreate(Db *, size_t,...);
|
extern DbRef * DbCreate(Db *, size_t,...);
|
||||||
|
|
||||||
/**
|
|
||||||
* Behaves like
|
|
||||||
* .Fn DbCreate ,
|
|
||||||
* but with an array of strings instead of variadic arguments.
|
|
||||||
*/
|
|
||||||
extern DbRef * DbCreateArgs(Db *, Array *);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lock an existing object in the database. This function will fail
|
* Lock an existing object in the database. This function will fail
|
||||||
* if the object does not exist. It takes a variable number of C
|
* if the object does not exist. It takes a variable number of C
|
||||||
|
@ -136,13 +129,6 @@ extern DbRef * DbCreateArgs(Db *, Array *);
|
||||||
*/
|
*/
|
||||||
extern DbRef * DbLock(Db *, size_t,...);
|
extern DbRef * DbLock(Db *, size_t,...);
|
||||||
|
|
||||||
/**
|
|
||||||
* Behaves like
|
|
||||||
* .Fn DbLock ,
|
|
||||||
* but with an array of strings instead of variadic arguments.
|
|
||||||
*/
|
|
||||||
extern DbRef * DbLockArgs(Db *, Array *);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Behaves like
|
* Behaves like
|
||||||
* .Fn DbLock ,
|
* .Fn DbLock ,
|
||||||
|
@ -152,13 +138,6 @@ extern DbRef * DbLockArgs(Db *, Array *);
|
||||||
*/
|
*/
|
||||||
extern DbRef * DbLockIntent(Db *, DbHint, size_t,...);
|
extern DbRef * DbLockIntent(Db *, DbHint, size_t,...);
|
||||||
|
|
||||||
/**
|
|
||||||
* Behaves like
|
|
||||||
* .Fn DbLockIntent ,
|
|
||||||
* but with an array of strings instead of variadic arguments.
|
|
||||||
*/
|
|
||||||
extern DbRef * DbLockIntentArgs(Db *, DbHint, Array *);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Immediately and permanently remove an object from the database.
|
* Immediately and permanently remove an object from the database.
|
||||||
* This function assumes the object is not locked, otherwise undefined
|
* This function assumes the object is not locked, otherwise undefined
|
||||||
|
@ -166,13 +145,6 @@ extern DbRef * DbLockIntentArgs(Db *, DbHint, Array *);
|
||||||
*/
|
*/
|
||||||
extern bool DbDelete(Db *, size_t,...);
|
extern bool DbDelete(Db *, size_t,...);
|
||||||
|
|
||||||
/**
|
|
||||||
* Behaves like
|
|
||||||
* .Fn DbDelete ,
|
|
||||||
* but with an array of strings instead of variadic arguments.
|
|
||||||
*/
|
|
||||||
extern bool DbDeleteArgs(Db *, Array *);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unlock an object and return it back to the database. This function
|
* Unlock an object and return it back to the database. This function
|
||||||
* immediately syncs the object to the filesystem. The cache is a
|
* immediately syncs the object to the filesystem. The cache is a
|
||||||
|
@ -190,13 +162,6 @@ extern bool DbUnlock(Db *, DbRef *);
|
||||||
*/
|
*/
|
||||||
extern bool DbExists(Db *, size_t,...);
|
extern bool DbExists(Db *, size_t,...);
|
||||||
|
|
||||||
/**
|
|
||||||
* Behaves like
|
|
||||||
* .Fn DbExists ,
|
|
||||||
* but with an array of strings instead of variadic arguments.
|
|
||||||
*/
|
|
||||||
extern bool DbExistsArgs(Db *, Array *);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List all of the objects at a given path. Unlike the other varargs
|
* List all of the objects at a given path. Unlike the other varargs
|
||||||
* functions, this one does not take a path to a specific object; it
|
* functions, this one does not take a path to a specific object; it
|
||||||
|
@ -209,13 +174,6 @@ extern bool DbExistsArgs(Db *, Array *);
|
||||||
*/
|
*/
|
||||||
extern Array * DbList(Db *, size_t,...);
|
extern Array * DbList(Db *, size_t,...);
|
||||||
|
|
||||||
/**
|
|
||||||
* Behaves like
|
|
||||||
* .Fn DbList ,
|
|
||||||
* but with an array of strings instead of variadic arguments.
|
|
||||||
*/
|
|
||||||
extern Array * DbListArgs(Db *, Array *);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Free the list returned by
|
* Free the list returned by
|
||||||
* .Fn DbListFree .
|
* .Fn DbListFree .
|
||||||
|
|
|
@ -297,12 +297,6 @@ extern size_t JsonEncode(HashMap *, Stream *, int);
|
||||||
*/
|
*/
|
||||||
extern HashMap * JsonDecode(Stream *);
|
extern HashMap * JsonDecode(Stream *);
|
||||||
|
|
||||||
/**
|
|
||||||
* Decodes a JSON value from thr current input strram and parse it
|
|
||||||
* into a JsonValue.
|
|
||||||
*/
|
|
||||||
extern JsonValue * JsonValueDecode(Stream *);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A convenience function that allows the caller to retrieve and
|
* A convenience function that allows the caller to retrieve and
|
||||||
* arbitrarily deep keys within a JSON object. It takes a root JSON
|
* arbitrarily deep keys within a JSON object. It takes a root JSON
|
||||||
|
|
|
@ -1,69 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_PLATFORM_H
|
|
||||||
#define CYTOPLASM_PLATFORM_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Platform
|
|
||||||
* @Nd A simple macro header that determines what platform the application
|
|
||||||
* is being built for.
|
|
||||||
* @Dd September 21, 2024
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
|
||||||
#define PLATFORM_WINDOWS
|
|
||||||
|
|
||||||
#ifdef _WIN64
|
|
||||||
#define PLATFORM_WIN64
|
|
||||||
#else
|
|
||||||
#define PLATFORM_WIN32
|
|
||||||
#endif
|
|
||||||
#elif __APPLE__
|
|
||||||
#define PLATFORM_DARWIN
|
|
||||||
|
|
||||||
#include <TargetConditionals.h>
|
|
||||||
#if TARGET_IPHONE_SIMULATOR
|
|
||||||
#define PLATFORM_IPHONE
|
|
||||||
#elif TARGET_OS_MACCATALYST
|
|
||||||
#define PLATFORM_CATALYST
|
|
||||||
#elif TARGET_OS_IPHONE
|
|
||||||
#define PLATFORM_IPHONE
|
|
||||||
#elif TARGET_OS_MAC
|
|
||||||
#define PLATFORM_MAC
|
|
||||||
#else
|
|
||||||
# error "Unknown Apple platform"
|
|
||||||
#endif
|
|
||||||
#elif __ANDROID__
|
|
||||||
#define PLATFORM_ANDROID
|
|
||||||
#elif __linux__
|
|
||||||
#define PLATFORM_LINUX
|
|
||||||
#elif __unix__
|
|
||||||
#define PLATFORM_UNIX
|
|
||||||
#elif defined(_POSIX_VERSION)
|
|
||||||
#define PLATFORM_POSIX
|
|
||||||
#else
|
|
||||||
# error "Unknown compiler"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_PLATFORM_H */
|
|
|
@ -41,16 +41,6 @@
|
||||||
* due to the lack of a 64-bit integer type, so that hash
|
* due to the lack of a 64-bit integer type, so that hash
|
||||||
* function has been omitted.
|
* function has been omitted.
|
||||||
*/
|
*/
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is an enum to be used to identify the type of SHA used.
|
|
||||||
*/
|
|
||||||
typedef enum HashType
|
|
||||||
{
|
|
||||||
HASH_SHA1,
|
|
||||||
HASH_SHA256
|
|
||||||
} HashType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function takes a pointer to a NULL-terminated C string, and
|
* This function takes a pointer to a NULL-terminated C string, and
|
||||||
|
@ -74,20 +64,6 @@ extern unsigned char * Sha256(char *);
|
||||||
*/
|
*/
|
||||||
extern unsigned char * Sha1(char *);
|
extern unsigned char * Sha1(char *);
|
||||||
|
|
||||||
/**
|
|
||||||
* This function behaves just like
|
|
||||||
* .Fn Sha256 ,
|
|
||||||
* except that it allows for a generic byte array, instead of a string.
|
|
||||||
*/
|
|
||||||
extern unsigned char * Sha256Raw(unsigned char *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function behaves just like
|
|
||||||
* .Fn Sha1 ,
|
|
||||||
* except that it allows for a generic byte array, instead of a string.
|
|
||||||
*/
|
|
||||||
extern unsigned char * Sha1Raw(unsigned char *, size_t);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a SHA byte buffer into a hex string. These hex strings
|
* Convert a SHA byte buffer into a hex string. These hex strings
|
||||||
* are typically what is transmitted, stored, and compared, however
|
* are typically what is transmitted, stored, and compared, however
|
||||||
|
@ -95,6 +71,6 @@ extern unsigned char * Sha1Raw(unsigned char *, size_t);
|
||||||
* bytes directly, which is why the conversion to a hex string is
|
* bytes directly, which is why the conversion to a hex string is
|
||||||
* a separate step.
|
* a separate step.
|
||||||
*/
|
*/
|
||||||
extern char * ShaToHex(unsigned char *, HashType);
|
extern char * ShaToHex(unsigned char *);
|
||||||
|
|
||||||
#endif /* CYTOPLASM_SHA_H */
|
#endif /* CYTOPLASM_SHA_H */
|
||||||
|
|
75
src/Db/Db.c
75
src/Db/Db.c
|
@ -262,16 +262,6 @@ DbClose(Db * db)
|
||||||
Free(db);
|
Free(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
DbRef *
|
|
||||||
DbCreateArgs(Db * db, Array *args)
|
|
||||||
{
|
|
||||||
if (!args || !db || !db->create)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return db->create(db, args);
|
|
||||||
}
|
|
||||||
DbRef *
|
DbRef *
|
||||||
DbCreate(Db * db, size_t nArgs,...)
|
DbCreate(Db * db, size_t nArgs,...)
|
||||||
{
|
{
|
||||||
|
@ -293,22 +283,12 @@ DbCreate(Db * db, size_t nArgs,...)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = DbCreateArgs(db, args);
|
ret = db->create(db, args);
|
||||||
ArrayFree(args);
|
ArrayFree(args);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
|
||||||
DbDeleteArgs(Db * db, Array *args)
|
|
||||||
{
|
|
||||||
if (!args || !db || !db->delete)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return db->delete(db, args);
|
|
||||||
}
|
|
||||||
bool
|
bool
|
||||||
DbDelete(Db * db, size_t nArgs,...)
|
DbDelete(Db * db, size_t nArgs,...)
|
||||||
{
|
{
|
||||||
|
@ -325,22 +305,12 @@ DbDelete(Db * db, size_t nArgs,...)
|
||||||
args = ArrayFromVarArgs(nArgs, ap);
|
args = ArrayFromVarArgs(nArgs, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
ret = DbDeleteArgs(db, args);
|
ret = db->delete(db, args);
|
||||||
|
|
||||||
ArrayFree(args);
|
ArrayFree(args);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
DbRef *
|
|
||||||
DbLockArgs(Db * db, Array *args)
|
|
||||||
{
|
|
||||||
if (!args || !db || !db->lockFunc)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return db->lockFunc(db, DB_HINT_WRITE, args);
|
|
||||||
}
|
|
||||||
DbRef *
|
DbRef *
|
||||||
DbLock(Db * db, size_t nArgs,...)
|
DbLock(Db * db, size_t nArgs,...)
|
||||||
{
|
{
|
||||||
|
@ -348,11 +318,6 @@ DbLock(Db * db, size_t nArgs,...)
|
||||||
Array *args;
|
Array *args;
|
||||||
DbRef *ret;
|
DbRef *ret;
|
||||||
|
|
||||||
if (!db)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
va_start(ap, nArgs);
|
va_start(ap, nArgs);
|
||||||
args = ArrayFromVarArgs(nArgs, ap);
|
args = ArrayFromVarArgs(nArgs, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
@ -362,22 +327,13 @@ DbLock(Db * db, size_t nArgs,...)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = DbLockArgs(db, args);
|
ret = db->lockFunc(db, DB_HINT_WRITE, args);
|
||||||
|
|
||||||
ArrayFree(args);
|
ArrayFree(args);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
DbRef *
|
|
||||||
DbLockIntentArgs(Db * db, DbHint hint, Array *args)
|
|
||||||
{
|
|
||||||
if (!db || !args || !db->lockFunc)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return db->lockFunc(db, hint, args);
|
|
||||||
}
|
|
||||||
DbRef *
|
DbRef *
|
||||||
DbLockIntent(Db * db, DbHint hint, size_t nArgs,...)
|
DbLockIntent(Db * db, DbHint hint, size_t nArgs,...)
|
||||||
{
|
{
|
||||||
|
@ -394,7 +350,7 @@ DbLockIntent(Db * db, DbHint hint, size_t nArgs,...)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = DbLockIntentArgs(db, hint, args);
|
ret = db->lockFunc(db, hint, args);
|
||||||
|
|
||||||
ArrayFree(args);
|
ArrayFree(args);
|
||||||
|
|
||||||
|
@ -412,16 +368,6 @@ DbUnlock(Db * db, DbRef * ref)
|
||||||
return db->unlock(db, ref);
|
return db->unlock(db, ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
|
||||||
DbExistsArgs(Db * db, Array *args)
|
|
||||||
{
|
|
||||||
if (!args || !db || !db->exists)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return db->exists(db, args);
|
|
||||||
}
|
|
||||||
bool
|
bool
|
||||||
DbExists(Db * db, size_t nArgs,...)
|
DbExists(Db * db, size_t nArgs,...)
|
||||||
{
|
{
|
||||||
|
@ -442,21 +388,12 @@ DbExists(Db * db, size_t nArgs,...)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = DbExistsArgs(db, args);
|
ret = db->exists(db, args);
|
||||||
ArrayFree(args);
|
ArrayFree(args);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
Array *
|
|
||||||
DbListArgs(Db *db, Array *args)
|
|
||||||
{
|
|
||||||
if (!db || !args || !db->list)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return db->list(db, args);
|
|
||||||
}
|
|
||||||
Array *
|
Array *
|
||||||
DbList(Db * db, size_t nArgs,...)
|
DbList(Db * db, size_t nArgs,...)
|
||||||
{
|
{
|
||||||
|
@ -473,7 +410,7 @@ DbList(Db * db, size_t nArgs,...)
|
||||||
path = ArrayFromVarArgs(nArgs, ap);
|
path = ArrayFromVarArgs(nArgs, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
result = DbListArgs(db, path);
|
result = db->list(db, path);
|
||||||
|
|
||||||
ArrayFree(path);
|
ArrayFree(path);
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -54,8 +54,7 @@ DbDirName(FlatDb * db, Array * args, size_t strip)
|
||||||
{
|
{
|
||||||
char *tmp;
|
char *tmp;
|
||||||
char *sanitise = StrDuplicate(ArrayGet(args, i));
|
char *sanitise = StrDuplicate(ArrayGet(args, i));
|
||||||
size_t len = strlen(sanitise);
|
for (j = 0; j < strlen(sanitise); j++)
|
||||||
for (j = 0; j < len; j++)
|
|
||||||
{
|
{
|
||||||
sanitise[j] = DbSanitiseChar(sanitise[j]);
|
sanitise[j] = DbSanitiseChar(sanitise[j]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,9 +128,7 @@ LMDBKeyHead(MDB_val key)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Doing >= will lead to cases where it is sent straight to the
|
while ((void *) (end - 1) >= key.mv_data && *(end - 1))
|
||||||
* start. Don't do that. */
|
|
||||||
while ((void *) (end - 1) > key.mv_data && *(end - 1))
|
|
||||||
{
|
{
|
||||||
end--;
|
end--;
|
||||||
}
|
}
|
||||||
|
@ -295,7 +293,6 @@ LMDBUnlock(Db *d, DbRef *r)
|
||||||
Stream *stream;
|
Stream *stream;
|
||||||
MDB_val key, val;
|
MDB_val key, val;
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
DbHint hint = r ? r->hint : 0;
|
|
||||||
|
|
||||||
if (!d || !r)
|
if (!d || !r)
|
||||||
{
|
{
|
||||||
|
@ -327,7 +324,7 @@ LMDBUnlock(Db *d, DbRef *r)
|
||||||
{
|
{
|
||||||
free(val.mv_data);
|
free(val.mv_data);
|
||||||
}
|
}
|
||||||
if (ret && hint == DB_HINT_WRITE)
|
if (ret && r->hint == DB_HINT_WRITE)
|
||||||
{
|
{
|
||||||
pthread_mutex_unlock(&d->lock);
|
pthread_mutex_unlock(&d->lock);
|
||||||
}
|
}
|
||||||
|
|
19
src/Json.c
19
src/Json.c
|
@ -1348,25 +1348,6 @@ JsonDecode(Stream * stream)
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
JsonValue *
|
|
||||||
JsonValueDecode(Stream *stream)
|
|
||||||
{
|
|
||||||
JsonValue *result;
|
|
||||||
JsonParserState state;
|
|
||||||
|
|
||||||
state.stream = stream;
|
|
||||||
state.token = NULL;
|
|
||||||
|
|
||||||
JsonTokenSeek(&state);
|
|
||||||
result = JsonDecodeValue(&state);
|
|
||||||
|
|
||||||
if (state.token)
|
|
||||||
{
|
|
||||||
Free(state.token);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonValue *
|
JsonValue *
|
||||||
JsonGet(HashMap * json, size_t nArgs,...)
|
JsonGet(HashMap * json, size_t nArgs,...)
|
||||||
|
|
272
src/Memory.c
272
src/Memory.c
|
@ -40,54 +40,29 @@
|
||||||
#define MEMORY_HEXDUMP_WIDTH 16
|
#define MEMORY_HEXDUMP_WIDTH 16
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MEMORY_FILE_SIZE 256
|
|
||||||
|
|
||||||
#define MEM_BOUND_TYPE uint64_t
|
|
||||||
#define MEM_BOUND 0xDEADBEEFBEEFDEAD
|
|
||||||
#define MEM_MAGIC 0xDEADBEEFDEADBEEF
|
|
||||||
|
|
||||||
struct MemoryInfo
|
struct MemoryInfo
|
||||||
{
|
{
|
||||||
uint64_t magic;
|
|
||||||
|
|
||||||
size_t size;
|
size_t size;
|
||||||
size_t unalignedSize;
|
const char *file;
|
||||||
char file[MEMORY_FILE_SIZE];
|
|
||||||
int line;
|
int line;
|
||||||
void *pointer;
|
void *pointer;
|
||||||
|
|
||||||
MemoryInfo *prev;
|
|
||||||
MemoryInfo *next;
|
|
||||||
|
|
||||||
MEM_BOUND_TYPE leftBoundary;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#define MEM_SIZE_ACTUAL(x) (MemoryAlignBoundary((x) * sizeof(uint8_t)) + sizeof(MEM_BOUND_TYPE))
|
#define MEM_BOUND_TYPE uint32_t
|
||||||
#define MEM_START_BOUNDARY(info) (info->leftBoundary)
|
#define MEM_BOUND 0xDEADBEEF
|
||||||
#define MEM_END_BOUNDARY(info) (*(((MEM_BOUND_TYPE *) (((uint8_t *) info->pointer) + info->size)) - 1))
|
|
||||||
|
|
||||||
|
#define MEM_BOUND_LOWER(p) *((MEM_BOUND_TYPE *) p)
|
||||||
|
#define MEM_BOUND_UPPER(p, x) *(((MEM_BOUND_TYPE *) (((uint8_t *) p) + x)) + 1)
|
||||||
|
#define MEM_SIZE_ACTUAL(x) (((x) * sizeof(uint8_t)) + (2 * sizeof(MEM_BOUND_TYPE)))
|
||||||
|
|
||||||
static pthread_mutex_t lock;
|
static pthread_mutex_t lock;
|
||||||
static void (*hook) (MemoryAction, MemoryInfo *, void *) = MemoryDefaultHook;
|
static void (*hook) (MemoryAction, MemoryInfo *, void *) = MemoryDefaultHook;
|
||||||
static void *hookArgs = NULL;
|
static void *hookArgs = NULL;
|
||||||
|
|
||||||
|
static MemoryInfo **allocations = NULL;
|
||||||
|
static size_t allocationsSize = 0;
|
||||||
static size_t allocationsLen = 0;
|
static size_t allocationsLen = 0;
|
||||||
|
|
||||||
static MemoryInfo *allocationTail = NULL;
|
|
||||||
|
|
||||||
/* Simple range of "plausible" boundaries for heap, serving as an extra
|
|
||||||
* check */
|
|
||||||
static void *heapStart, *heapEnd;
|
|
||||||
|
|
||||||
static size_t MemoryAlignBoundary(size_t size)
|
|
||||||
{
|
|
||||||
size_t boundSize = sizeof(MEM_BOUND_TYPE);
|
|
||||||
size_t remainder = size % boundSize;
|
|
||||||
size_t closest = size / boundSize + !!remainder;
|
|
||||||
|
|
||||||
return closest * boundSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
MemoryRuntimeInit(void)
|
MemoryRuntimeInit(void)
|
||||||
{
|
{
|
||||||
|
@ -102,8 +77,6 @@ MemoryRuntimeInit(void)
|
||||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||||
ret = pthread_mutex_init(&lock, &attr);
|
ret = pthread_mutex_init(&lock, &attr);
|
||||||
pthread_mutexattr_destroy(&attr);
|
pthread_mutexattr_destroy(&attr);
|
||||||
heapStart = NULL;
|
|
||||||
heapEnd = NULL;
|
|
||||||
|
|
||||||
ret = (ret == 0);
|
ret = (ret == 0);
|
||||||
|
|
||||||
|
@ -118,27 +91,70 @@ MemoryRuntimeDestroy(void)
|
||||||
return pthread_mutex_destroy(&lock) == 0;
|
return pthread_mutex_destroy(&lock) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
MemoryHash(void *p)
|
||||||
|
{
|
||||||
|
return (((size_t) p) >> 2 * 7) % allocationsSize;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
MemoryInsert(MemoryInfo * a)
|
MemoryInsert(MemoryInfo * a)
|
||||||
{
|
{
|
||||||
if (allocationTail)
|
size_t hash;
|
||||||
{
|
|
||||||
allocationTail->next = a;
|
|
||||||
}
|
|
||||||
a->next = NULL;
|
|
||||||
a->prev = allocationTail;
|
|
||||||
a->magic = MEM_MAGIC;
|
|
||||||
|
|
||||||
if (!heapStart || heapStart > (void *) a)
|
if (!allocations)
|
||||||
{
|
{
|
||||||
heapStart = a;
|
allocationsSize = MEMORY_TABLE_CHUNK;
|
||||||
}
|
allocations = calloc(allocationsSize, sizeof(void *));
|
||||||
if (!heapEnd || heapEnd < (void *) a)
|
if (!allocations)
|
||||||
{
|
{
|
||||||
heapEnd = a;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
allocationTail = a;
|
/* If the next insertion would cause the table to be at least 3/4
|
||||||
|
* full, re-allocate and re-hash. */
|
||||||
|
if ((allocationsLen + 1) >= ((allocationsSize * 3) >> 2))
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
size_t tmpAllocationsSize = allocationsSize;
|
||||||
|
MemoryInfo **tmpAllocations;
|
||||||
|
|
||||||
|
allocationsSize += MEMORY_TABLE_CHUNK;
|
||||||
|
tmpAllocations = calloc(allocationsSize, sizeof(void *));
|
||||||
|
|
||||||
|
if (!tmpAllocations)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < tmpAllocationsSize; i++)
|
||||||
|
{
|
||||||
|
if (allocations[i])
|
||||||
|
{
|
||||||
|
hash = MemoryHash(allocations[i]->pointer);
|
||||||
|
|
||||||
|
while (tmpAllocations[hash])
|
||||||
|
{
|
||||||
|
hash = (hash + 1) % allocationsSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpAllocations[hash] = allocations[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(allocations);
|
||||||
|
allocations = tmpAllocations;
|
||||||
|
}
|
||||||
|
|
||||||
|
hash = MemoryHash(a->pointer);
|
||||||
|
|
||||||
|
while (allocations[hash])
|
||||||
|
{
|
||||||
|
hash = (hash + 1) % allocationsSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
allocations[hash] = a;
|
||||||
allocationsLen++;
|
allocationsLen++;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -147,32 +163,30 @@ MemoryInsert(MemoryInfo * a)
|
||||||
static void
|
static void
|
||||||
MemoryDelete(MemoryInfo * a)
|
MemoryDelete(MemoryInfo * a)
|
||||||
{
|
{
|
||||||
MemoryInfo *aPrev = a->prev;
|
size_t hash = MemoryHash(a->pointer);
|
||||||
MemoryInfo *aNext = a->next;
|
size_t count = 0;
|
||||||
|
|
||||||
if (aPrev)
|
while (count <= allocationsSize)
|
||||||
{
|
{
|
||||||
aPrev->next = aNext;
|
if (allocations[hash] && allocations[hash] == a)
|
||||||
|
{
|
||||||
|
allocations[hash] = NULL;
|
||||||
|
allocationsLen--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hash = (hash + 1) % allocationsSize;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (aNext)
|
|
||||||
{
|
|
||||||
aNext->prev = aPrev;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (a == allocationTail)
|
|
||||||
{
|
|
||||||
allocationTail = aPrev;
|
|
||||||
}
|
|
||||||
|
|
||||||
a->magic = ~MEM_MAGIC;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
MemoryCheck(MemoryInfo * a)
|
MemoryCheck(MemoryInfo * a)
|
||||||
{
|
{
|
||||||
if (MEM_START_BOUNDARY(a) != MEM_BOUND ||
|
if (MEM_BOUND_LOWER(a->pointer) != MEM_BOUND ||
|
||||||
a->magic != MEM_MAGIC ||
|
MEM_BOUND_UPPER(a->pointer, a->size - (2 * sizeof(MEM_BOUND_TYPE))) != MEM_BOUND)
|
||||||
MEM_END_BOUNDARY(a) != MEM_BOUND)
|
|
||||||
{
|
{
|
||||||
if (hook)
|
if (hook)
|
||||||
{
|
{
|
||||||
|
@ -189,32 +203,38 @@ MemoryAllocate(size_t size, const char *file, int line)
|
||||||
void *p;
|
void *p;
|
||||||
MemoryInfo *a;
|
MemoryInfo *a;
|
||||||
|
|
||||||
//MemoryIterate(NULL, NULL);
|
MemoryIterate(NULL, NULL);
|
||||||
|
|
||||||
pthread_mutex_lock(&lock);
|
pthread_mutex_lock(&lock);
|
||||||
|
|
||||||
a = malloc(sizeof(MemoryInfo) + MEM_SIZE_ACTUAL(size));
|
a = malloc(sizeof(MemoryInfo));
|
||||||
if (!a)
|
if (!a)
|
||||||
{
|
{
|
||||||
pthread_mutex_unlock(&lock);
|
pthread_mutex_unlock(&lock);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
p = a + 1;
|
p = malloc(MEM_SIZE_ACTUAL(size));
|
||||||
|
if (!p)
|
||||||
|
{
|
||||||
|
free(a);
|
||||||
|
pthread_mutex_unlock(&lock);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
memset(p, 0, MEM_SIZE_ACTUAL(size));
|
memset(p, 0, MEM_SIZE_ACTUAL(size));
|
||||||
|
MEM_BOUND_LOWER(p) = MEM_BOUND;
|
||||||
|
MEM_BOUND_UPPER(p, size) = MEM_BOUND;
|
||||||
|
|
||||||
a->size = MEM_SIZE_ACTUAL(size);
|
a->size = MEM_SIZE_ACTUAL(size);
|
||||||
a->unalignedSize = size;
|
a->file = file;
|
||||||
strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
|
|
||||||
a->line = line;
|
a->line = line;
|
||||||
a->pointer = p;
|
a->pointer = p;
|
||||||
MEM_START_BOUNDARY(a) = MEM_BOUND;
|
|
||||||
MEM_END_BOUNDARY(a) = MEM_BOUND;
|
|
||||||
|
|
||||||
if (!MemoryInsert(a))
|
if (!MemoryInsert(a))
|
||||||
{
|
{
|
||||||
free(a);
|
free(a);
|
||||||
|
free(p);
|
||||||
pthread_mutex_unlock(&lock);
|
pthread_mutex_unlock(&lock);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -225,7 +245,7 @@ MemoryAllocate(size_t size, const char *file, int line)
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_unlock(&lock);
|
pthread_mutex_unlock(&lock);
|
||||||
return p;
|
return ((MEM_BOUND_TYPE *) p) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *
|
void *
|
||||||
|
@ -234,7 +254,7 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
|
||||||
MemoryInfo *a;
|
MemoryInfo *a;
|
||||||
void *new = NULL;
|
void *new = NULL;
|
||||||
|
|
||||||
//MemoryIterate(NULL, NULL);
|
MemoryIterate(NULL, NULL);
|
||||||
|
|
||||||
if (!p)
|
if (!p)
|
||||||
{
|
{
|
||||||
|
@ -245,22 +265,19 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
|
||||||
if (a)
|
if (a)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&lock);
|
pthread_mutex_lock(&lock);
|
||||||
MemoryDelete(a);
|
new = realloc(a->pointer, MEM_SIZE_ACTUAL(size));
|
||||||
new = realloc(a, sizeof(MemoryInfo) + MEM_SIZE_ACTUAL(size));
|
|
||||||
if (new)
|
if (new)
|
||||||
{
|
{
|
||||||
a = new;
|
MemoryDelete(a);
|
||||||
a->unalignedSize = size;
|
|
||||||
a->size = MEM_SIZE_ACTUAL(size);
|
a->size = MEM_SIZE_ACTUAL(size);
|
||||||
strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
|
a->file = file;
|
||||||
a->line = line;
|
a->line = line;
|
||||||
a->magic = MEM_MAGIC;
|
|
||||||
|
|
||||||
a->pointer = a + 1;
|
a->pointer = new;
|
||||||
MemoryInsert(a);
|
MemoryInsert(a);
|
||||||
|
|
||||||
MEM_START_BOUNDARY(a) = MEM_BOUND;
|
MEM_BOUND_LOWER(a->pointer) = MEM_BOUND;
|
||||||
MEM_END_BOUNDARY(a) = MEM_BOUND;
|
MEM_BOUND_UPPER(a->pointer, size) = MEM_BOUND;
|
||||||
|
|
||||||
if (hook)
|
if (hook)
|
||||||
{
|
{
|
||||||
|
@ -276,7 +293,7 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
|
||||||
if (a)
|
if (a)
|
||||||
{
|
{
|
||||||
a->size = 0;
|
a->size = 0;
|
||||||
strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
|
a->file = file;
|
||||||
a->line = line;
|
a->line = line;
|
||||||
a->pointer = p;
|
a->pointer = p;
|
||||||
hook(MEMORY_BAD_POINTER, a, hookArgs);
|
hook(MEMORY_BAD_POINTER, a, hookArgs);
|
||||||
|
@ -284,7 +301,7 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return a->pointer;
|
return ((MEM_BOUND_TYPE *) new) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -292,7 +309,7 @@ MemoryFree(void *p, const char *file, int line)
|
||||||
{
|
{
|
||||||
MemoryInfo *a;
|
MemoryInfo *a;
|
||||||
|
|
||||||
//MemoryIterate(NULL, NULL);
|
MemoryIterate(NULL, NULL);
|
||||||
|
|
||||||
if (!p)
|
if (!p)
|
||||||
{
|
{
|
||||||
|
@ -305,11 +322,12 @@ MemoryFree(void *p, const char *file, int line)
|
||||||
pthread_mutex_lock(&lock);
|
pthread_mutex_lock(&lock);
|
||||||
if (hook)
|
if (hook)
|
||||||
{
|
{
|
||||||
strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
|
a->file = file;
|
||||||
a->line = line;
|
a->line = line;
|
||||||
hook(MEMORY_FREE, a, hookArgs);
|
hook(MEMORY_FREE, a, hookArgs);
|
||||||
}
|
}
|
||||||
MemoryDelete(a);
|
MemoryDelete(a);
|
||||||
|
free(a->pointer);
|
||||||
free(a);
|
free(a);
|
||||||
|
|
||||||
pthread_mutex_unlock(&lock);
|
pthread_mutex_unlock(&lock);
|
||||||
|
@ -319,7 +337,7 @@ MemoryFree(void *p, const char *file, int line)
|
||||||
a = malloc(sizeof(MemoryInfo));
|
a = malloc(sizeof(MemoryInfo));
|
||||||
if (a)
|
if (a)
|
||||||
{
|
{
|
||||||
strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
|
a->file = file;
|
||||||
a->line = line;
|
a->line = line;
|
||||||
a->size = 0;
|
a->size = 0;
|
||||||
a->pointer = p;
|
a->pointer = p;
|
||||||
|
@ -332,15 +350,17 @@ MemoryFree(void *p, const char *file, int line)
|
||||||
size_t
|
size_t
|
||||||
MemoryAllocated(void)
|
MemoryAllocated(void)
|
||||||
{
|
{
|
||||||
|
size_t i;
|
||||||
size_t total = 0;
|
size_t total = 0;
|
||||||
MemoryInfo *cur;
|
|
||||||
|
|
||||||
pthread_mutex_lock(&lock);
|
pthread_mutex_lock(&lock);
|
||||||
|
|
||||||
/* TODO */
|
for (i = 0; i < allocationsSize; i++)
|
||||||
for (cur = allocationTail; cur; cur = cur->prev)
|
|
||||||
{
|
{
|
||||||
total += MemoryInfoGetSize(cur);
|
if (allocations[i])
|
||||||
|
{
|
||||||
|
total += MemoryInfoGetSize(allocations[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_unlock(&lock);
|
pthread_mutex_unlock(&lock);
|
||||||
|
@ -351,44 +371,55 @@ MemoryAllocated(void)
|
||||||
void
|
void
|
||||||
MemoryFreeAll(void)
|
MemoryFreeAll(void)
|
||||||
{
|
{
|
||||||
MemoryInfo *cur;
|
size_t i;
|
||||||
MemoryInfo *prev;
|
|
||||||
|
|
||||||
pthread_mutex_lock(&lock);
|
pthread_mutex_lock(&lock);
|
||||||
|
|
||||||
/* TODO */
|
for (i = 0; i < allocationsSize; i++)
|
||||||
for (cur = allocationTail; cur; cur = prev)
|
|
||||||
{
|
{
|
||||||
prev = cur->prev;
|
if (allocations[i])
|
||||||
free(cur);
|
{
|
||||||
|
free(allocations[i]->pointer);
|
||||||
|
free(allocations[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(allocations);
|
||||||
|
allocations = NULL;
|
||||||
|
allocationsSize = 0;
|
||||||
allocationsLen = 0;
|
allocationsLen = 0;
|
||||||
|
|
||||||
pthread_mutex_unlock(&lock);
|
pthread_mutex_unlock(&lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
MemoryInfo *
|
MemoryInfo *
|
||||||
MemoryInfoGet(void *po)
|
MemoryInfoGet(void *p)
|
||||||
{
|
{
|
||||||
void *p = po;
|
size_t hash, count;
|
||||||
|
|
||||||
pthread_mutex_lock(&lock);
|
pthread_mutex_lock(&lock);
|
||||||
|
|
||||||
p = ((MemoryInfo *) p) - 1;
|
p = ((MEM_BOUND_TYPE *) p) - 1;
|
||||||
if (p < heapStart || p > heapEnd)
|
hash = MemoryHash(p);
|
||||||
{
|
|
||||||
pthread_mutex_unlock(&lock);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (((MemoryInfo *)p)->magic != MEM_MAGIC)
|
count = 0;
|
||||||
|
while (count <= allocationsSize)
|
||||||
{
|
{
|
||||||
p = NULL;
|
if (!allocations[hash] || allocations[hash]->pointer != p)
|
||||||
|
{
|
||||||
|
hash = (hash + 1) % allocationsSize;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MemoryCheck(allocations[hash]);
|
||||||
|
pthread_mutex_unlock(&lock);
|
||||||
|
return allocations[hash];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_unlock(&lock);
|
pthread_mutex_unlock(&lock);
|
||||||
return p;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
|
@ -399,7 +430,7 @@ MemoryInfoGetSize(MemoryInfo * a)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return a->size ? a->unalignedSize : 0;
|
return a->size ? a->size - (2 * sizeof(MEM_BOUND_TYPE)) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
|
@ -432,22 +463,25 @@ MemoryInfoGetPointer(MemoryInfo * a)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return a->pointer;
|
return ((MEM_BOUND_TYPE *) a->pointer) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
MemoryIterate(void (*iterFunc) (MemoryInfo *, void *), void *args)
|
MemoryIterate(void (*iterFunc) (MemoryInfo *, void *), void *args)
|
||||||
{
|
{
|
||||||
MemoryInfo *cur;
|
size_t i;
|
||||||
|
|
||||||
pthread_mutex_lock(&lock);
|
pthread_mutex_lock(&lock);
|
||||||
|
|
||||||
for (cur = allocationTail; cur; cur = cur->prev)
|
for (i = 0; i < allocationsSize; i++)
|
||||||
{
|
{
|
||||||
MemoryCheck(cur);
|
if (allocations[i])
|
||||||
if (iterFunc)
|
|
||||||
{
|
{
|
||||||
iterFunc(cur, args);
|
MemoryCheck(allocations[i]);
|
||||||
|
if (iterFunc)
|
||||||
|
{
|
||||||
|
iterFunc(allocations[i], args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
33
src/Sha.c
33
src/Sha.c
|
@ -28,44 +28,21 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
char *
|
char *
|
||||||
ShaToHex(unsigned char *bytes, HashType type)
|
ShaToHex(unsigned char *bytes)
|
||||||
{
|
{
|
||||||
size_t i = 0, size;
|
size_t i = 0;
|
||||||
char *str;
|
char *str = Malloc(((strlen((char *) bytes) * 2) + 1) * sizeof(char));
|
||||||
|
|
||||||
switch (type)
|
|
||||||
{
|
|
||||||
case HASH_SHA1:
|
|
||||||
size = 20;
|
|
||||||
break;
|
|
||||||
case HASH_SHA256:
|
|
||||||
size = 32;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
str = Malloc(((size * 2) + 1) * sizeof(char));
|
|
||||||
if (!str)
|
if (!str)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < size; i++)
|
while (bytes[i] != '\0')
|
||||||
{
|
{
|
||||||
snprintf(str + (2 * i), 3, "%02x", bytes[i]);
|
snprintf(str + (2 * i), 3, "%02x", bytes[i]);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
unsigned char *
|
|
||||||
Sha256(char *str)
|
|
||||||
{
|
|
||||||
return Sha256Raw((unsigned char *) str, str ? strlen(str) : 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char *
|
|
||||||
Sha1(char *str)
|
|
||||||
{
|
|
||||||
return Sha1Raw((unsigned char *) str, str ? strlen(str) : 0);
|
|
||||||
}
|
|
||||||
|
|
|
@ -28,27 +28,6 @@
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
/* TODO: Verify LibreSSL support later */
|
|
||||||
#if defined(TLS_IMPL) && (TLS_IMPL == TLS_OPENSSL)
|
|
||||||
|
|
||||||
#include <openssl/sha.h>
|
|
||||||
|
|
||||||
unsigned char *
|
|
||||||
Sha1Raw(unsigned char *str, size_t len)
|
|
||||||
{
|
|
||||||
unsigned char *digest;
|
|
||||||
if (!str)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
digest = Malloc(20 + 1);
|
|
||||||
SHA1(str, len, digest);
|
|
||||||
digest[20] = '\0';
|
|
||||||
return digest;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
|
|
||||||
#define LOAD32H(x, y) \
|
#define LOAD32H(x, y) \
|
||||||
{ \
|
{ \
|
||||||
x = ((uint32_t)((y)[0] & 255) << 24) | \
|
x = ((uint32_t)((y)[0] & 255) << 24) | \
|
||||||
|
@ -261,7 +240,7 @@ Sha1Calculate(Sha1Context * ctx, unsigned char *out)
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *
|
unsigned char *
|
||||||
Sha1Raw(unsigned char *str, size_t len)
|
Sha1(char *str)
|
||||||
{
|
{
|
||||||
Sha1Context ctx;
|
Sha1Context ctx;
|
||||||
unsigned char *out;
|
unsigned char *out;
|
||||||
|
@ -278,11 +257,10 @@ Sha1Raw(unsigned char *str, size_t len)
|
||||||
}
|
}
|
||||||
|
|
||||||
Sha1Init(&ctx);
|
Sha1Init(&ctx);
|
||||||
Sha1Update(&ctx, str, len);
|
Sha1Update(&ctx, str, strlen(str));
|
||||||
Sha1Calculate(&ctx, out);
|
Sha1Calculate(&ctx, out);
|
||||||
|
|
||||||
out[160 / 8] = '\0';
|
out[160 / 8] = '\0';
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
|
@ -21,36 +21,14 @@
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include <Memory.h>
|
|
||||||
#include <Sha.h>
|
#include <Sha.h>
|
||||||
|
#include <Memory.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
|
|
||||||
/* TODO: Verify LibreSSL support later */
|
|
||||||
#if defined(TLS_IMPL) && (TLS_IMPL == TLS_OPENSSL)
|
|
||||||
|
|
||||||
#include <openssl/sha.h>
|
|
||||||
|
|
||||||
unsigned char *
|
|
||||||
Sha256Raw(unsigned char *str, size_t len)
|
|
||||||
{
|
|
||||||
unsigned char *digest;
|
|
||||||
if (!str)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
digest = Malloc(32 + 1);
|
|
||||||
SHA256(str, len, digest);
|
|
||||||
digest[32] = '\0';
|
|
||||||
return digest;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
|
|
||||||
#define GET_UINT32(x) \
|
#define GET_UINT32(x) \
|
||||||
(((uint32_t)(x)[0] << 24) | \
|
(((uint32_t)(x)[0] << 24) | \
|
||||||
((uint32_t)(x)[1] << 16) | \
|
((uint32_t)(x)[1] << 16) | \
|
||||||
|
@ -192,7 +170,7 @@ Sha256Process(Sha256Context * context, unsigned char *data, size_t length)
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *
|
unsigned char *
|
||||||
Sha256Raw(unsigned char *str, size_t len)
|
Sha256(char *str)
|
||||||
{
|
{
|
||||||
Sha256Context context;
|
Sha256Context context;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
@ -228,7 +206,7 @@ Sha256Raw(unsigned char *str, size_t len)
|
||||||
context.length = 0;
|
context.length = 0;
|
||||||
memset(context.buffer, 0, 64);
|
memset(context.buffer, 0, 64);
|
||||||
|
|
||||||
Sha256Process(&context, str, len);
|
Sha256Process(&context, (unsigned char *) str, strlen(str));
|
||||||
|
|
||||||
memset(fill, 0, 64);
|
memset(fill, 0, 64);
|
||||||
fill[0] = 0x80;
|
fill[0] = 0x80;
|
||||||
|
@ -252,4 +230,3 @@ Sha256Raw(unsigned char *str, size_t len)
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
*/
|
*/
|
||||||
#include <Tls.h>
|
#include <Tls.h>
|
||||||
|
|
||||||
#if defined(TLS_IMPL) && (TLS_IMPL == TLS_LIBRESSL)
|
#if TLS_IMPL == TLS_LIBRESSL
|
||||||
|
|
||||||
#include <Memory.h>
|
#include <Memory.h>
|
||||||
#include <Log.h>
|
#include <Log.h>
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
*/
|
*/
|
||||||
#include <Tls.h>
|
#include <Tls.h>
|
||||||
|
|
||||||
#if defined(TLS_IMPL) && (TLS_IMPL == TLS_OPENSSL)
|
#if TLS_IMPL == TLS_OPENSSL
|
||||||
|
|
||||||
#include <Memory.h>
|
#include <Memory.h>
|
||||||
#include <Log.h>
|
#include <Log.h>
|
||||||
|
|
|
@ -24,7 +24,6 @@
|
||||||
#include <Util.h>
|
#include <Util.h>
|
||||||
|
|
||||||
#include <Memory.h>
|
#include <Memory.h>
|
||||||
#include <Platform.h>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -90,10 +89,6 @@ UtilTsMillis(void)
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef PLATFORM_DARWIN
|
|
||||||
#define st_mtim st_mtimespec
|
|
||||||
#endif
|
|
||||||
|
|
||||||
uint64_t
|
uint64_t
|
||||||
UtilLastModified(char *path)
|
UtilLastModified(char *path)
|
||||||
{
|
{
|
||||||
|
@ -110,10 +105,6 @@ UtilLastModified(char *path)
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef PLATFORM_DARWIN
|
|
||||||
#undef st_mtim
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int
|
int
|
||||||
UtilMkdir(const char *dir, const mode_t mode)
|
UtilMkdir(const char *dir, const mode_t mode)
|
||||||
{
|
{
|
||||||
|
|
271
tools/j2s.c
271
tools/j2s.c
|
@ -38,7 +38,6 @@
|
||||||
|
|
||||||
#define MAX_DEPENDENCIES 32
|
#define MAX_DEPENDENCIES 32
|
||||||
|
|
||||||
#define IsDelimited(field, c1, c2) (*field == c1 && field[strlen(field) - 1] == c2)
|
|
||||||
static char *
|
static char *
|
||||||
Trim(char c, char *str)
|
Trim(char c, char *str)
|
||||||
{
|
{
|
||||||
|
@ -76,14 +75,10 @@ TypeToJsonType(char *type)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (IsDelimited(type, '[', ']'))
|
if (*type == '[' && type[strlen(type) - 1] == ']')
|
||||||
{
|
{
|
||||||
return JSON_ARRAY;
|
return JSON_ARRAY;
|
||||||
}
|
}
|
||||||
else if (IsDelimited(type, '{', '}'))
|
|
||||||
{
|
|
||||||
return JSON_OBJECT;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return JSON_OBJECT;
|
return JSON_OBJECT;
|
||||||
|
@ -96,7 +91,7 @@ JsonTypeToStr(JsonType type)
|
||||||
{
|
{
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case JSON_OBJECT:
|
case JSON_OBJECT:
|
||||||
return "JSON_OBJECT";
|
return "JSON_OBJECT";
|
||||||
case JSON_ARRAY:
|
case JSON_ARRAY:
|
||||||
return "JSON_ARRAY";
|
return "JSON_ARRAY";
|
||||||
|
@ -330,7 +325,6 @@ Main(Array * args)
|
||||||
{
|
{
|
||||||
char *fieldType;
|
char *fieldType;
|
||||||
bool isArrType = false;
|
bool isArrType = false;
|
||||||
bool isObjType = false;
|
|
||||||
JsonValue *requiredVal;
|
JsonValue *requiredVal;
|
||||||
JsonValue *ignoreVal;
|
JsonValue *ignoreVal;
|
||||||
|
|
||||||
|
@ -349,18 +343,12 @@ Main(Array * args)
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsDelimited(fieldType, '[', ']'))
|
if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
|
||||||
{
|
{
|
||||||
fieldType++;
|
fieldType++;
|
||||||
fieldType[strlen(fieldType) - 1] = '\0';
|
fieldType[strlen(fieldType) - 1] = '\0';
|
||||||
isArrType = true;
|
isArrType = true;
|
||||||
}
|
}
|
||||||
else if (IsDelimited(fieldType, '{', '}'))
|
|
||||||
{
|
|
||||||
fieldType++;
|
|
||||||
fieldType[strlen(fieldType) - 1] = '\0';
|
|
||||||
isObjType = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!StrEquals(fieldType, "object") &&
|
if (!StrEquals(fieldType, "object") &&
|
||||||
!StrEquals(fieldType, "array") &&
|
!StrEquals(fieldType, "array") &&
|
||||||
|
@ -386,10 +374,6 @@ Main(Array * args)
|
||||||
{
|
{
|
||||||
fieldType[strlen(fieldType)] = ']';
|
fieldType[strlen(fieldType)] = ']';
|
||||||
}
|
}
|
||||||
else if (isObjType)
|
|
||||||
{
|
|
||||||
fieldType[strlen(fieldType)] = '}';
|
|
||||||
}
|
|
||||||
|
|
||||||
requiredVal = HashMapGet(fieldObj, "required");
|
requiredVal = HashMapGet(fieldObj, "required");
|
||||||
if (requiredVal && JsonValueType(requiredVal) != JSON_BOOLEAN)
|
if (requiredVal && JsonValueType(requiredVal) != JSON_BOOLEAN)
|
||||||
|
@ -545,13 +529,11 @@ Main(Array * args)
|
||||||
{
|
{
|
||||||
cType = "double";
|
cType = "double";
|
||||||
}
|
}
|
||||||
else if (StrEquals(fieldType, "object") ||
|
else if (StrEquals(fieldType, "object"))
|
||||||
IsDelimited(fieldType, '{', '}'))
|
|
||||||
{
|
{
|
||||||
cType = "HashMap *";
|
cType = "HashMap *";
|
||||||
}
|
}
|
||||||
else if (StrEquals(fieldType, "array") ||
|
else if (StrEquals(fieldType, "array") || (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']'))
|
||||||
IsDelimited(fieldType, '[', ']'))
|
|
||||||
{
|
{
|
||||||
cType = "Array *";
|
cType = "Array *";
|
||||||
}
|
}
|
||||||
|
@ -560,20 +542,7 @@ Main(Array * args)
|
||||||
cType = fieldType;
|
cType = fieldType;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsDelimited(fieldType, '{', '}') ||
|
StreamPrintf(headerFile, " %s %s;\n", cType, field);
|
||||||
IsDelimited(fieldType, '[', ']'))
|
|
||||||
{
|
|
||||||
char end = fieldType[strlen(fieldType) - 1];
|
|
||||||
fieldType[strlen(fieldType) - 1] = '\0';
|
|
||||||
|
|
||||||
StreamPrintf(headerFile, " %s /* of %s */ %s;\n", cType, fieldType + 1, field);
|
|
||||||
|
|
||||||
fieldType[strlen(fieldType)] = end;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
StreamPrintf(headerFile, " %s %s;\n", cType, field);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StreamPrintf(headerFile, "} %s;\n\n", type);
|
StreamPrintf(headerFile, "} %s;\n\n", type);
|
||||||
|
@ -704,134 +673,7 @@ Main(Array * args)
|
||||||
StreamPrintf(implFile, " out->%s = JsonValueAsObject(val);\n", key);
|
StreamPrintf(implFile, " out->%s = JsonValueAsObject(val);\n", key);
|
||||||
StreamPrintf(implFile, " Free(val); /* Not JsonValueFree() because we want the inner value. */\n");
|
StreamPrintf(implFile, " Free(val); /* Not JsonValueFree() because we want the inner value. */\n");
|
||||||
}
|
}
|
||||||
else if (IsDelimited(fieldType, '{', '}'))
|
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
|
||||||
{
|
|
||||||
fieldType++;
|
|
||||||
fieldType[strlen(fieldType) - 1] = '\0';
|
|
||||||
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
|
||||||
jsonType = isEnum ? JSON_STRING : TypeToJsonType(fieldType);
|
|
||||||
|
|
||||||
StreamPrintf(implFile, " out->%s = HashMapCreate();\n", key);
|
|
||||||
StreamPrintf(implFile, " if (!out->%s)\n", key);
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
StreamPrintf(implFile, " *errp = \"Failed to allocate memory for %s.%s.\";\n", type, key);
|
|
||||||
StreamPrintf(implFile, " return false;\n");
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
StreamPrintf(implFile, " else\n");
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
StreamPrintf(implFile, " HashMap *obj = JsonValueAsObject(val);\n");
|
|
||||||
StreamPrintf(implFile, " char *objKey;\n");
|
|
||||||
StreamPrintf(implFile, " JsonValue *v;\n");
|
|
||||||
StreamPrintf(implFile, "\n");
|
|
||||||
StreamPrintf(implFile, " while (HashMapIterate(obj, &objKey, (void **) &v))\n");
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
|
|
||||||
if (StrEquals(fieldType, "integer") ||
|
|
||||||
StrEquals(fieldType, "float") ||
|
|
||||||
StrEquals(fieldType, "boolean"))
|
|
||||||
{
|
|
||||||
char *cType;
|
|
||||||
|
|
||||||
if (StrEquals(fieldType, "integer"))
|
|
||||||
{
|
|
||||||
cType = "int64_t";
|
|
||||||
}
|
|
||||||
else if (StrEquals(fieldType, "float"))
|
|
||||||
{
|
|
||||||
cType = "double";
|
|
||||||
}
|
|
||||||
else if (StrEquals(fieldType, "boolean"))
|
|
||||||
{
|
|
||||||
cType = "bool";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Should never happen */
|
|
||||||
cType = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
*fieldType = toupper(*fieldType);
|
|
||||||
|
|
||||||
StreamPrintf(implFile, " %s *ref;\n", cType);
|
|
||||||
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
StreamPrintf(implFile, " *errp = \"%s.%s{} contains an invalid value.\";\n", type, key);
|
|
||||||
StreamPrintf(implFile, " return false;\n");
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
StreamPrintf(implFile, " ref = Malloc(sizeof(%s));\n", cType);
|
|
||||||
StreamPrintf(implFile, " if (!ref)\n");
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
StreamPrintf(implFile, " *errp = \"Unable to allocate memory for object value.\";\n");
|
|
||||||
StreamPrintf(implFile, " return false;\n");
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
StreamPrintf(implFile, " *ref = JsonValueAs%s(v);\n", fieldType);
|
|
||||||
StreamPrintf(implFile, " HashMapSet(out->%s, objKey, ref);\n", key);
|
|
||||||
|
|
||||||
*fieldType = tolower(*fieldType);
|
|
||||||
}
|
|
||||||
else if (StrEquals(fieldType, "string"))
|
|
||||||
{
|
|
||||||
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
|
|
||||||
StreamPrintf(implFile, " return false;\n");
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
StreamPrintf(implFile, " HashMapSet(out->%s, objKey, StrDuplicate(JsonValueAsString(v)));\n", key);
|
|
||||||
}
|
|
||||||
else if (StrEquals(fieldType, "object"))
|
|
||||||
{
|
|
||||||
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
|
|
||||||
StreamPrintf(implFile, " return false;\n");
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
StreamPrintf(implFile, " HashMapSet(out->%s, objKey, JsonDuplicate(JsonValueAsObject(v)));\n", key);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (isEnum)
|
|
||||||
{
|
|
||||||
StreamPrintf(implFile, " int parseResult;\n");
|
|
||||||
}
|
|
||||||
StreamPrintf(implFile, " %s *parsed;\n", fieldType);
|
|
||||||
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
|
|
||||||
StreamPrintf(implFile, " return false;\n");
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
StreamPrintf(implFile, " parsed = Malloc(sizeof(%s));\n", fieldType);
|
|
||||||
StreamPrintf(implFile, " if (!parsed)\n");
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
StreamPrintf(implFile, " *errp = \"Unable to allocate memory for array value.\";\n");
|
|
||||||
StreamPrintf(implFile, " return false;\n");
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
if (isEnum)
|
|
||||||
{
|
|
||||||
StreamPrintf(implFile, " parseResult = %sFromStr(JsonValueAsString(v));\n", fieldType);
|
|
||||||
StreamPrintf(implFile, " *parsed = parseResult;\n");
|
|
||||||
StreamPrintf(implFile, " if (parseResult == -1)\n");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
StreamPrintf(implFile, " if (!%sFromJson(JsonValueAsObject(v), parsed, errp))\n", fieldType);
|
|
||||||
}
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
if (!isEnum)
|
|
||||||
{
|
|
||||||
StreamPrintf(implFile, " %sFree(parsed);\n", fieldType);
|
|
||||||
}
|
|
||||||
StreamPrintf(implFile, " Free(parsed);\n");
|
|
||||||
StreamPrintf(implFile, " return false;\n");
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
StreamPrintf(implFile, " HashMapSet(out->%s, objKey, parsed);\n", key);
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
|
|
||||||
fieldType[strlen(fieldType)] = '}';
|
|
||||||
}
|
|
||||||
else if (IsDelimited(fieldType, '[', ']'))
|
|
||||||
{
|
{
|
||||||
fieldType++;
|
fieldType++;
|
||||||
fieldType[strlen(fieldType) - 1] = '\0';
|
fieldType[strlen(fieldType) - 1] = '\0';
|
||||||
|
@ -1073,73 +915,7 @@ Main(Array * args)
|
||||||
{
|
{
|
||||||
StreamPrintf(implFile, " HashMapSet(json, \"%s\", JsonValueObject(JsonDuplicate(val->%s)));\n", Trim('_', key), key);
|
StreamPrintf(implFile, " HashMapSet(json, \"%s\", JsonValueObject(JsonDuplicate(val->%s)));\n", Trim('_', key), key);
|
||||||
}
|
}
|
||||||
else if (IsDelimited(fieldType, '{', '}'))
|
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
|
||||||
{
|
|
||||||
int isPrimitive;
|
|
||||||
|
|
||||||
fieldType++;
|
|
||||||
fieldType[strlen(fieldType) - 1] = '\0';
|
|
||||||
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
|
||||||
isPrimitive = StrEquals(fieldType, "integer") ||
|
|
||||||
StrEquals(fieldType, "boolean") ||
|
|
||||||
StrEquals(fieldType, "float");
|
|
||||||
|
|
||||||
|
|
||||||
StreamPrintf(implFile, " if (val->%s)\n", key);
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
StreamPrintf(implFile, " char *objKey;\n");
|
|
||||||
StreamPrintf(implFile, " void *objVal;\n");
|
|
||||||
StreamPrintf(implFile, " HashMap *jsonObj = HashMapCreate();\n");
|
|
||||||
StreamPrintf(implFile, " if (!jsonObj)\n");
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
StreamPrintf(implFile, " JsonFree(json);\n");
|
|
||||||
StreamPrintf(implFile, " return NULL;\n");
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
StreamPrintf(implFile, " while (HashMapIterate(val->%s, &objKey, &objVal))\n", key);
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
|
|
||||||
if (StrEquals(fieldType, "string"))
|
|
||||||
{
|
|
||||||
StreamPrintf(implFile, " HashMapSet(jsonObj, objKey, JsonValueString(objVal));\n", key);
|
|
||||||
}
|
|
||||||
else if (!isPrimitive)
|
|
||||||
{
|
|
||||||
StreamPrintf(implFile, " HashMapSet(jsonObj, objKey, JsonValueObject(%sToJson(objVal)));\n", fieldType, key);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char *cType;
|
|
||||||
|
|
||||||
if (StrEquals(fieldType, "integer"))
|
|
||||||
{
|
|
||||||
cType = "int64_t";
|
|
||||||
}
|
|
||||||
else if (StrEquals(fieldType, "float"))
|
|
||||||
{
|
|
||||||
cType = "double";
|
|
||||||
}
|
|
||||||
else if (StrEquals(fieldType, "boolean"))
|
|
||||||
{
|
|
||||||
cType = "bool";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Should never happen */
|
|
||||||
cType = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
*fieldType = toupper(*fieldType);
|
|
||||||
StreamPrintf(implFile, " HashMapSet(jsonObj, objKey, (JsonValue%s(*((%s *) objVal))));\n", fieldType, cType, key);
|
|
||||||
*fieldType = tolower(*fieldType);
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
StreamPrintf(implFile, " HashMapSet(json, \"%s\", JsonValueObject(jsonObj));\n", Trim('_', key));
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
|
|
||||||
fieldType[strlen(fieldType)] = '}';
|
|
||||||
}
|
|
||||||
else if (IsDelimited(fieldType, '[', ']'))
|
|
||||||
{
|
{
|
||||||
int isPrimitive;
|
int isPrimitive;
|
||||||
|
|
||||||
|
@ -1260,36 +1036,7 @@ Main(Array * args)
|
||||||
{
|
{
|
||||||
StreamPrintf(implFile, " Free(val->%s);\n", key);
|
StreamPrintf(implFile, " Free(val->%s);\n", key);
|
||||||
}
|
}
|
||||||
else if (IsDelimited(fieldType, '{', '}'))
|
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
|
||||||
{
|
|
||||||
int isPrimitive;
|
|
||||||
|
|
||||||
fieldType++;
|
|
||||||
fieldType[strlen(fieldType) - 1] = '\0';
|
|
||||||
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
|
||||||
isPrimitive = StrEquals(fieldType, "boolean") ||
|
|
||||||
StrEquals(fieldType, "float") ||
|
|
||||||
StrEquals(fieldType, "integer") ||
|
|
||||||
StrEquals(fieldType, "string");
|
|
||||||
|
|
||||||
StreamPrintf(implFile, " if (val->%s)\n", key);
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
StreamPrintf(implFile, " char *objKey;\n");
|
|
||||||
StreamPrintf(implFile, " void *objVal;\n");
|
|
||||||
StreamPrintf(implFile, " while (HashMapIterate(val->%s, &objKey, &objVal))\n", key);
|
|
||||||
StreamPrintf(implFile, " {\n");
|
|
||||||
StreamPrintf(implFile, " %sFree(objVal);\n", (!isEnum && !isPrimitive) ? fieldType : "", key);
|
|
||||||
if (!isEnum && !isPrimitive)
|
|
||||||
{
|
|
||||||
StreamPrintf(implFile, " Free(objVal);\n", key);
|
|
||||||
}
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
StreamPrintf(implFile, " HashMapFree(val->%s);\n", key);
|
|
||||||
StreamPrintf(implFile, " }\n");
|
|
||||||
|
|
||||||
fieldType[strlen(fieldType)] = '}';
|
|
||||||
}
|
|
||||||
else if (IsDelimited(fieldType, '[', ']'))
|
|
||||||
{
|
{
|
||||||
int isPrimitive;
|
int isPrimitive;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue