Skip to content

Commit b78dd79

Browse files
committed
* BREAKING API CHANGE: allow client to specify memory allocation routines
* bump yajl version to 1.0.0 * update TODO list, removing completed tasks * update yajl_test to override allocation routines and count allocations/frees, providing an automated standalone way to validate we're not leaky * update all copyright dates * update all .gold files with new test output * update ChangeLog in preparation for 1.0.0 release
1 parent af30552 commit b78dd79

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+404
-72
lines changed

CMakeLists.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2007, Lloyd Hilaiel.
1+
# Copyright 2007-2009, Lloyd Hilaiel.
22
#
33
# Redistribution and use in source and binary forms, with or without
44
# modification, are permitted provided that the following conditions are
@@ -32,9 +32,9 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
3232

3333
PROJECT(YetAnotherJSONParser)
3434

35-
SET (YAJL_MAJOR 0)
36-
SET (YAJL_MINOR 4)
37-
SET (YAJL_MICRO 1)
35+
SET (YAJL_MAJOR 1)
36+
SET (YAJL_MINOR 0)
37+
SET (YAJL_MICRO 0)
3838

3939
SET (YAJL_DIST_NAME "yajl-${YAJL_MAJOR}.${YAJL_MINOR}.${YAJL_MICRO}")
4040

COPYING

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2007, Lloyd Hilaiel.
1+
Copyright 2007-2009, Lloyd Hilaiel.
22

33
Redistribution and use in source and binary forms, with or without
44
modification, are permitted provided that the following conditions are

ChangeLog

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
1.0.0
22
* lth add 'make install' rules, thaks to Andrei Soroker for the
33
contribution.
4+
* lth client may override allocation routines at generator or parser
5+
allocation time
6+
* tjw add yajl_parse_complete routine to allow client to explicitly
7+
specify end-of-input, solving the "lonely number" case, where
8+
json text consists only of an element with no explicit syntactic
9+
end.
10+
* tjw many new test cases
11+
* tjw cleanup of code for symmetry and ease of reading
12+
* lth integration of patches from Robert Varga which cleanup
13+
compilation warnings on 64 bit linux
14+
415
0.4.0
516
* lth buffer overflow bug in yajl_gen_double s/%lf/%g/ - thanks to
617
Eric Bergstrome

TODO

-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
* add a test for 0x1F bug
22
* numeric overflow in integers and double
33
* line and char offsets in the lexer and in error messages
4-
* numbers at eof bug. client must have a way of indicating 'end of input'
5-
(other than passing in a ' ' whitespace byte),
6-
problem illustrated with lonely number test case.
74
* testing:
85
a. the permuter
96
b. some performance comparison against json_checker.
107
* investigate pull instead of push parsing
118
* Handle memory allocation failures gracefully
12-
* allow client to override malloc/calloc/free impl.
139
* cygwin/msys support on win32

configure

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env ruby
2-
# Copyright 2007, Lloyd Hilaiel.
2+
# Copyright 2007-2009, Lloyd Hilaiel.
33
#
44
# Redistribution and use in source and binary forms, with or without
55
# modification, are permitted provided that the following conditions are

reformatter/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2007, Lloyd Hilaiel.
1+
# Copyright 2007-2009, Lloyd Hilaiel.
22
#
33
# Redistribution and use in source and binary forms, with or without
44
# modification, are permitted provided that the following conditions are

reformatter/json_reformat.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2007, Lloyd Hilaiel.
2+
* Copyright 2007-2009, Lloyd Hilaiel.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are
@@ -156,10 +156,10 @@ main(int argc, char ** argv)
156156
usage(argv[0]);
157157
}
158158

159-
g = yajl_gen_alloc(&conf);
159+
g = yajl_gen_alloc(&conf, NULL);
160160

161161
/* ok. open file. let's read and parse */
162-
hand = yajl_alloc(&callbacks, &cfg, (void *) g);
162+
hand = yajl_alloc(&callbacks, &cfg, (void *) g, NULL);
163163

164164
while (!done) {
165165
rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);
@@ -185,7 +185,7 @@ main(int argc, char ** argv)
185185
{
186186
unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
187187
fprintf(stderr, (const char *) str);
188-
yajl_free_error(str);
188+
yajl_free_error(hand, str);
189189
} else {
190190
const unsigned char * buf;
191191
unsigned int len;

src/CMakeLists.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2007, Lloyd Hilaiel.
1+
# Copyright 2007-2009, Lloyd Hilaiel.
22
#
33
# Redistribution and use in source and binary forms, with or without
44
# modification, are permitted provided that the following conditions are
@@ -28,8 +28,9 @@
2828
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2929
# POSSIBILITY OF SUCH DAMAGE.
3030

31-
SET (SRCS yajl.c yajl_lex.c yajl_parser.c yajl_buf.c yajl_encode.c yajl_gen.c)
32-
SET (HDRS yajl_parser.h yajl_lex.h yajl_buf.h yajl_encode.h)
31+
SET (SRCS yajl.c yajl_lex.c yajl_parser.c yajl_buf.c
32+
yajl_encode.c yajl_gen.c yajl_alloc.c)
33+
SET (HDRS yajl_parser.h yajl_lex.h yajl_buf.h yajl_encode.h yajl_alloc.h)
3334
SET (PUB_HDRS api/yajl_parse.h api/yajl_gen.h api/yajl_common.h)
3435

3536
# useful when fixing lexer bugs.

src/api/yajl_common.h

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2007, Lloyd Hilaiel.
2+
* Copyright 2007-2009, Lloyd Hilaiel.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are
@@ -33,6 +33,10 @@
3333
#ifndef __YAJL_COMMON_H__
3434
#define __YAJL_COMMON_H__
3535

36+
#ifdef __cplusplus
37+
extern "C" {
38+
#endif
39+
3640
#define YAJL_MAX_DEPTH 128
3741

3842
/* msft dll export gunk. To build a DLL on windows, you
@@ -48,4 +52,34 @@
4852
# define YAJL_API
4953
#endif
5054

55+
/** pointer to a malloc function, supporting client overriding memory
56+
* allocation routines */
57+
typedef void * (*yajl_malloc_func)(void *ctx, unsigned int sz);
58+
59+
/** pointer to a free function, supporting client overriding memory
60+
* allocation routines */
61+
typedef void (*yajl_free_func)(void *ctx, void * ptr);
62+
63+
/** pointer to a realloc function which can resize an allocation. */
64+
typedef void * (*yajl_realloc_func)(void *ctx, void * ptr, unsigned int sz);
65+
66+
/** A structure which can be passed to yajl_*_alloc routines to allow the
67+
* client to specify memory allocation functions to be used. */
68+
typedef struct
69+
{
70+
/** pointer to a function that can allocate uninitialized memory */
71+
yajl_malloc_func malloc;
72+
/** pointer to a function that can resize memory allocations */
73+
yajl_realloc_func realloc;
74+
/** pointer to a function that can free memory allocated using
75+
* reallocFunction or mallocFunction */
76+
yajl_free_func free;
77+
/** a context pointer that will be passed to above allocation routines */
78+
void * ctx;
79+
} yajl_alloc_funcs;
80+
81+
#ifdef __cplusplus
82+
};
83+
#endif
84+
5185
#endif

src/api/yajl_gen.h

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2007, Lloyd Hilaiel.
2+
* Copyright 2007-2009, Lloyd Hilaiel.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are
@@ -73,8 +73,18 @@ extern "C" {
7373
const char * indentString;
7474
} yajl_gen_config;
7575

76-
/** allocate a generator handle */
77-
yajl_gen YAJL_API yajl_gen_alloc(const yajl_gen_config * config);
76+
/** allocate a generator handle
77+
* \param config a pointer to a structure containing parameters which
78+
* configure the behavior of the json generator
79+
* \param allocFuncs an optional pointer to a structure which allows
80+
* the client to overide the memory allocation
81+
* used by yajl. May be NULL, in which case
82+
* malloc/free/realloc will be used.
83+
*
84+
* \returns an allocated handle on success, NULL on failure (bad params)
85+
*/
86+
yajl_gen YAJL_API yajl_gen_alloc(const yajl_gen_config * config,
87+
const yajl_alloc_funcs * allocFuncs);
7888

7989
/** free a generator handle */
8090
void YAJL_API yajl_gen_free(yajl_gen handle);

src/api/yajl_parse.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2007, Lloyd Hilaiel.
2+
* Copyright 2007-2009, Lloyd Hilaiel.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are
@@ -129,6 +129,7 @@ extern "C" {
129129
*/
130130
yajl_handle YAJL_API yajl_alloc(const yajl_callbacks * callbacks,
131131
const yajl_parser_config * config,
132+
const yajl_alloc_funcs * allocFuncs,
132133
void * ctx);
133134

134135
/** free a parser handle */
@@ -169,7 +170,7 @@ extern "C" {
169170
unsigned int jsonTextLength);
170171

171172
/** free an error returned from yajl_get_error */
172-
void YAJL_API yajl_free_error(unsigned char * str);
173+
void YAJL_API yajl_free_error(yajl_handle hand, unsigned char * str);
173174

174175
#ifdef __cplusplus
175176
};

src/yajl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
\mainpage Yet Another JSON Library (YAJL)
33
\author Lloyd Hilaiel
4-
\date 2007-2008
4+
\date 2007-2009
55

66
Yet Another JSON Library (YAJL) is a small event-driven (SAX-style)
77
JSON parser written in ANSI C, and a small validating JSON

src/yajl.c

+28-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2007, Lloyd Hilaiel.
2+
* Copyright 2007-2009, Lloyd Hilaiel.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are
@@ -33,6 +33,7 @@
3333
#include "api/yajl_parse.h"
3434
#include "yajl_lex.h"
3535
#include "yajl_parser.h"
36+
#include "yajl_alloc.h"
3637

3738
#include <stdlib.h>
3839
#include <string.h>
@@ -62,11 +63,29 @@ yajl_status_to_string(yajl_status stat)
6263
yajl_handle
6364
yajl_alloc(const yajl_callbacks * callbacks,
6465
const yajl_parser_config * config,
66+
const yajl_alloc_funcs * afs,
6567
void * ctx)
6668
{
6769
unsigned int allowComments = 0;
6870
unsigned int validateUTF8 = 0;
69-
yajl_handle hand = (yajl_handle) malloc(sizeof(struct yajl_handle_t));
71+
yajl_handle hand = NULL;
72+
yajl_alloc_funcs afsBuffer;
73+
74+
/* first order of business is to set up memory allocation routines */
75+
if (afs != NULL) {
76+
if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
77+
{
78+
return NULL;
79+
}
80+
} else {
81+
yajl_set_default_alloc_funcs(&afsBuffer);
82+
afs = &afsBuffer;
83+
}
84+
85+
hand = (yajl_handle) YA_MALLOC(afs, sizeof(struct yajl_handle_t));
86+
87+
/* copy in pointers to allocation routines */
88+
memcpy((void *) &(hand->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
7089

7190
if (config != NULL) {
7291
allowComments = config->allowComments;
@@ -75,10 +94,10 @@ yajl_alloc(const yajl_callbacks * callbacks,
7594

7695
hand->callbacks = callbacks;
7796
hand->ctx = ctx;
78-
hand->lexer = yajl_lex_alloc(allowComments, validateUTF8);
97+
hand->lexer = yajl_lex_alloc(&(hand->alloc), allowComments, validateUTF8);
7998
hand->errorOffset = 0;
80-
hand->decodeBuf = yajl_buf_alloc();
81-
hand->stateBuf = yajl_buf_alloc();
99+
hand->decodeBuf = yajl_buf_alloc(&(hand->alloc));
100+
hand->stateBuf = yajl_buf_alloc(&(hand->alloc));
82101

83102
yajl_state_push(hand, yajl_state_start);
84103

@@ -91,7 +110,7 @@ yajl_free(yajl_handle handle)
91110
yajl_buf_free(handle->stateBuf);
92111
yajl_buf_free(handle->decodeBuf);
93112
yajl_lex_free(handle->lexer);
94-
free(handle);
113+
YA_FREE(&(handle->alloc), handle);
95114
}
96115

97116
yajl_status
@@ -124,10 +143,10 @@ yajl_get_error(yajl_handle hand, int verbose,
124143
}
125144

126145
void
127-
yajl_free_error(unsigned char * str)
146+
yajl_free_error(yajl_handle hand, unsigned char * str)
128147
{
129-
/* XXX: use memory allocation functions if set */
130-
free(str);
148+
/* use memory allocation functions if set */
149+
YA_FREE(&(hand->alloc), str);
131150
}
132151

133152
/* XXX: add utility routines to parse from file */

src/yajl_alloc.c

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2007-2009, Lloyd Hilaiel.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in
13+
* the documentation and/or other materials provided with the
14+
* distribution.
15+
*
16+
* 3. Neither the name of Lloyd Hilaiel nor the names of its
17+
* contributors may be used to endorse or promote products derived
18+
* from this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
/**
34+
* \file yajl_alloc.h
35+
* default memory allocation routines for yajl which use malloc/realloc and
36+
* free
37+
*/
38+
39+
#include "yajl_alloc.h"
40+
#include <stdlib.h>
41+
42+
static void * yajl_internal_malloc(void *ctx, unsigned int sz)
43+
{
44+
return malloc(sz);
45+
}
46+
47+
static void * yajl_internal_realloc(void *ctx, void * previous,
48+
unsigned int sz)
49+
{
50+
return realloc(previous, sz);
51+
}
52+
53+
static void yajl_internal_free(void *ctx, void * ptr)
54+
{
55+
free(ptr);
56+
}
57+
58+
void yajl_set_default_alloc_funcs(yajl_alloc_funcs * yaf)
59+
{
60+
yaf->malloc = yajl_internal_malloc;
61+
yaf->free = yajl_internal_free;
62+
yaf->realloc = yajl_internal_realloc;
63+
yaf->ctx = NULL;
64+
}
65+

0 commit comments

Comments
 (0)