Skip to content

Commit 1498b78

Browse files
committed
Minor simplifications.
1 parent 85e84fc commit 1498b78

File tree

1 file changed

+3
-20
lines changed

1 file changed

+3
-20
lines changed

include/simdjson/padded_string.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,25 @@
55
#include <memory>
66
#include <string>
77

8-
// padded buffer can be made with length < 1
9-
#define SIMDJSON_OK_EMPTY_PADDED_BUFFER 1
108

119
namespace simdjson {
1210
// low-level function to allocate memory with padding so we can read passed the
1311
// "length" bytes safely. if you must provide a pointer to some data, create it
1412
// with this function: length is the max. size in bytes of the string caller is
1513
// responsible to free the memory (free(...))
1614
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-
2815
// we could do a simple malloc
2916
// return (char *) malloc(length + SIMDJSON_PADDING);
3017
// However, we might as well align to cache lines...
3118
size_t totalpaddedlength = length + SIMDJSON_PADDING;
3219
char *padded_buffer = aligned_malloc_char(64, totalpaddedlength);
33-
34-
#ifndef NDEBUG
20+
#ifndef NDEBUG
3521
if (padded_buffer == nullptr) {
3622
errno = EINVAL;
3723
perror("simdjson::allocate_padded_buffer() aligned_malloc_char() failed");
3824
return nullptr;
3925
}
4026
#endif // NDEBUG
41-
4227
memset(padded_buffer + length, 0, totalpaddedlength - length);
4328
return padded_buffer;
4429
} // allocate_padded_buffer
@@ -52,14 +37,13 @@ struct padded_string final {
5237

5338
explicit padded_string(size_t length) noexcept
5439
: viable_size(length), data_ptr(allocate_padded_buffer(length)) {
55-
5640
if (data_ptr != nullptr)
5741
data_ptr[length] = '\0'; // easier when you need a c_str
5842
}
5943

6044
explicit padded_string(char *data, size_t length) noexcept
6145
: viable_size(length), data_ptr(allocate_padded_buffer(length)) {
62-
if (data != nullptr) {
46+
if ((data != nullptr) and (data_ptr != nullptr)) {
6347
memcpy(data_ptr, data, length);
6448
data_ptr[length] = '\0'; // easier when you need a c_str
6549
}
@@ -107,7 +91,6 @@ struct padded_string final {
10791

10892
~padded_string() {
10993
aligned_free_char(data_ptr);
110-
this->data_ptr = nullptr;
11194
}
11295

11396
size_t size() const { return viable_size; }
@@ -120,7 +103,7 @@ struct padded_string final {
120103
padded_string &operator=(const padded_string &o) = delete;
121104
padded_string(const padded_string &o) = delete;
122105

123-
size_t viable_size ;
106+
size_t viable_size;
124107
char *data_ptr{nullptr};
125108

126109
}; // padded_string

0 commit comments

Comments
 (0)