Skip to content

Commit b768196

Browse files
lightyear15thiagomacieira
authored andcommitted
follow-up commit to 75eaa19
facilitate replacement of malloc/free functions open_memstream functions are left out of this change as they require other functions from stdlib.
1 parent 4dcad26 commit b768196

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

src/cborparser_dup_string.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,8 @@
3535

3636
#include "cbor.h"
3737
#include "compilersupport_p.h"
38+
#include "memory.h"
3839

39-
#if defined(CBOR_CUSTOM_ALLOC_INCLUDE)
40-
# include CBOR_CUSTOM_ALLOC_INCLUDE
41-
#else
42-
# include <stdlib.h>
43-
# define cbor_malloc malloc
44-
# define cbor_free free
45-
#endif
4640

4741
/**
4842
* \fn CborError cbor_value_dup_text_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)

src/cbortojson.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "cborinternal_p.h"
3636
#include "compilersupport_p.h"
3737
#include "cborinternal_p.h"
38+
#include <memory.h>
3839

3940
#include <inttypes.h>
4041
#include <stdio.h>
@@ -179,7 +180,7 @@ static CborError dump_bytestring_base16(char **result, CborValue *it)
179180
return err;
180181

181182
/* a Base16 (hex) output is twice as big as our buffer */
182-
buffer = (uint8_t *)malloc(n * 2 + 1);
183+
buffer = (uint8_t *)cbor_malloc(n * 2 + 1);
183184
if (buffer == NULL)
184185
/* out of memory */
185186
return CborErrorOutOfMemory;
@@ -209,7 +210,7 @@ static CborError generic_dump_base64(char **result, CborValue *it, const char al
209210

210211
/* a Base64 output (untruncated) has 4 bytes for every 3 in the input */
211212
size_t len = (n + 5) / 3 * 4;
212-
buffer = (uint8_t *)malloc(len + 1);
213+
buffer = (uint8_t *)cbor_malloc(len + 1);
213214
if (buffer == NULL)
214215
/* out of memory */
215216
return CborErrorOutOfMemory;
@@ -395,7 +396,7 @@ static CborError tagged_value_to_json(FILE *out, CborValue *it, int flags, Conve
395396
if (err)
396397
return err;
397398
err = fprintf(out, "\"%s%s\"", pre, str) < 0 ? CborErrorIO : CborNoError;
398-
free(str);
399+
cbor_free(str);
399400
status->flags = TypeWasNotNative | TypeWasTagged | CborByteStringType;
400401
return err;
401402
}
@@ -467,7 +468,7 @@ static CborError map_to_json(FILE *out, CborValue *it, int flags, ConversionStat
467468

468469
/* first, print the key */
469470
if (fprintf(out, "\"%s\":", key) < 0) {
470-
free(key);
471+
cbor_free(key);
471472
return CborErrorIO;
472473
}
473474

@@ -489,7 +490,7 @@ static CborError map_to_json(FILE *out, CborValue *it, int flags, ConversionStat
489490
}
490491
}
491492

492-
free(key);
493+
cbor_free(key);
493494
if (err)
494495
return err;
495496
}
@@ -568,7 +569,7 @@ static CborError value_to_json(FILE *out, CborValue *it, int flags, CborType typ
568569
if (err)
569570
return err;
570571
err = (fprintf(out, "\"%s\"", str) < 0) ? CborErrorIO : CborNoError;
571-
free(str);
572+
cbor_free(str);
572573
return err;
573574
}
574575

src/memory.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2016 Intel Corporation
4+
**
5+
** Permission is hereby granted, free of charge, to any person obtaining a copy
6+
** of this software and associated documentation files (the "Software"), to deal
7+
** in the Software without restriction, including without limitation the rights
8+
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
** copies of the Software, and to permit persons to whom the Software is
10+
** furnished to do so, subject to the following conditions:
11+
**
12+
** The above copyright notice and this permission notice shall be included in
13+
** all copies or substantial portions of the Software.
14+
**
15+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
** THE SOFTWARE.
22+
**
23+
****************************************************************************/
24+
25+
#if defined(CBOR_CUSTOM_ALLOC_INCLUDE)
26+
# include CBOR_CUSTOM_ALLOC_INCLUDE
27+
#else
28+
# include <stdlib.h>
29+
# define cbor_malloc malloc
30+
# define cbor_free free
31+
#endif

0 commit comments

Comments
 (0)