5
5
#include < memory>
6
6
#include < string>
7
7
8
- // padded buffer can be made with length < 1
9
- #define SIMDJSON_OK_EMPTY_PADDED_BUFFER 1
10
8
11
9
namespace simdjson {
12
10
// low-level function to allocate memory with padding so we can read passed the
13
11
// "length" bytes safely. if you must provide a pointer to some data, create it
14
12
// with this function: length is the max. size in bytes of the string caller is
15
13
// responsible to free the memory (free(...))
16
14
inline char *allocate_padded_buffer (size_t length) noexcept {
17
-
18
- #ifndef NDEBUG
19
- #ifndef SIMDJSON_OK_EMPTY_PADDED_BUFFER
20
- if (length < 1 ) {
21
- errno = EINVAL;
22
- perror (" simdjson::allocate_padded_buffer() length argument is less than 1" );
23
- return nullptr ;
24
- }
25
- #endif // SIMDJSON_OK_EMPTY_PADDED_STRING
26
- #endif // NDEBUG
27
-
28
15
// we could do a simple malloc
29
16
// return (char *) malloc(length + SIMDJSON_PADDING);
30
17
// However, we might as well align to cache lines...
31
18
size_t totalpaddedlength = length + SIMDJSON_PADDING;
32
19
char *padded_buffer = aligned_malloc_char (64 , totalpaddedlength);
33
-
34
- #ifndef NDEBUG
20
+ #ifndef NDEBUG
35
21
if (padded_buffer == nullptr ) {
36
22
errno = EINVAL;
37
23
perror (" simdjson::allocate_padded_buffer() aligned_malloc_char() failed" );
38
24
return nullptr ;
39
25
}
40
26
#endif // NDEBUG
41
-
42
27
memset (padded_buffer + length, 0 , totalpaddedlength - length);
43
28
return padded_buffer;
44
29
} // allocate_padded_buffer
@@ -52,14 +37,13 @@ struct padded_string final {
52
37
53
38
explicit padded_string (size_t length) noexcept
54
39
: viable_size(length), data_ptr(allocate_padded_buffer(length)) {
55
-
56
40
if (data_ptr != nullptr )
57
41
data_ptr[length] = ' \0 ' ; // easier when you need a c_str
58
42
}
59
43
60
44
explicit padded_string (char *data, size_t length) noexcept
61
45
: viable_size(length), data_ptr(allocate_padded_buffer(length)) {
62
- if (data != nullptr ) {
46
+ if (( data != nullptr ) and (data_ptr != nullptr ) ) {
63
47
memcpy (data_ptr, data, length);
64
48
data_ptr[length] = ' \0 ' ; // easier when you need a c_str
65
49
}
@@ -107,7 +91,6 @@ struct padded_string final {
107
91
108
92
~padded_string () {
109
93
aligned_free_char (data_ptr);
110
- this ->data_ptr = nullptr ;
111
94
}
112
95
113
96
size_t size () const { return viable_size; }
@@ -120,7 +103,7 @@ struct padded_string final {
120
103
padded_string &operator =(const padded_string &o) = delete ;
121
104
padded_string (const padded_string &o) = delete ;
122
105
123
- size_t viable_size ;
106
+ size_t viable_size;
124
107
char *data_ptr{nullptr };
125
108
126
109
}; // padded_string
0 commit comments