Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef NDN_DYNAMICUCHARARRAY_H |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 8 | #define NDN_DYNAMICUCHARARRAY_H |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 10 | #include "../errors.h" |
Jeff Thompson | 2630dd0 | 2013-06-28 19:40:18 -0700 | [diff] [blame] | 11 | #include "ndn_memory.h" |
| 12 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 13 | #ifdef __cplusplus |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 14 | extern "C" { |
| 15 | #endif |
| 16 | |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 17 | struct ndn_DynamicUInt8Array { |
| 18 | uint8_t *array; /**< the allocated array buffer */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 19 | size_t length; /**< the length of the allocated array buffer */ |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 20 | uint8_t * (*realloc) |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 21 | (struct ndn_DynamicUInt8Array *self, uint8_t *array, size_t length); /**< a pointer to a function that reallocates array and returns a new pointer to a buffer of |
Jeff Thompson | 9254d74 | 2013-08-12 12:04:44 -0700 | [diff] [blame] | 22 | * length bytes, or 0 for error. On success, the contents of the old buffer are copied to the new one. |
| 23 | * On success, the original array pointer will no longer be used. |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 24 | * self is a pointer to the struct ndn_DynamicUInt8Array which is calling realloc. |
Jeff Thompson | 9254d74 | 2013-08-12 12:04:44 -0700 | [diff] [blame] | 25 | * This function pointer may be 0 (which causes an error if a reallocate is necessary). */ |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 28 | /** |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 29 | * Initialize an ndn_DynamicUInt8Array struct with the given array buffer. |
| 30 | * @param self pointer to the ndn_DynamicUInt8Array struct |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 31 | * @param array the allocated array buffer |
| 32 | * @param length the length of the allocated array buffer |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 33 | * @param reallocFunction see ndn_DynamicUInt8Array_ensureLength. This may be 0. |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 34 | */ |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 35 | static inline void ndn_DynamicUInt8Array_initialize |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 36 | (struct ndn_DynamicUInt8Array *self, uint8_t *array, size_t length, |
| 37 | uint8_t * (*reallocFunction)(struct ndn_DynamicUInt8Array *self, uint8_t *, size_t)) |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 38 | { |
| 39 | self->array = array; |
| 40 | self->length = length; |
| 41 | self->realloc = reallocFunction; |
| 42 | } |
| 43 | |
Jeff Thompson | 4f93888 | 2013-07-01 16:01:09 -0700 | [diff] [blame] | 44 | /** |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 45 | * Do the work of ndn_DynamicUInt8Array_ensureLength if realloc is necessary. |
Jeff Thompson | 4f93888 | 2013-07-01 16:01:09 -0700 | [diff] [blame] | 46 | * If the self->realloc function pointer is null, then return an error. |
| 47 | * If not null, call self->realloc to reallocate self->array, and update self->length (which may be greater than length). |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 48 | * @param self pointer to the ndn_DynamicUInt8Array struct |
Jeff Thompson | 4f93888 | 2013-07-01 16:01:09 -0700 | [diff] [blame] | 49 | * @param length the needed minimum size for self->length |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 50 | * @return 0 for success, else an error code if can't reallocate the array |
Jeff Thompson | 4f93888 | 2013-07-01 16:01:09 -0700 | [diff] [blame] | 51 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 52 | ndn_Error ndn_DynamicUInt8Array_reallocArray(struct ndn_DynamicUInt8Array *self, size_t length); |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 53 | |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Ensure that self->length is greater than or equal to length. If it is, just return 0 for success. |
| 56 | * Otherwise, if the self->realloc function pointer is null, then return an error. |
| 57 | * If not null, call self->realloc to reallocate self->array, and update self->length (which may be greater than length). |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 58 | * @param self pointer to the ndn_DynamicUInt8Array struct |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 59 | * @param length the needed minimum size for self->length |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 60 | * @return 0 for success, else an error code if need to reallocate the array but can't |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 61 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 62 | static inline ndn_Error ndn_DynamicUInt8Array_ensureLength(struct ndn_DynamicUInt8Array *self, size_t length) |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 63 | { |
| 64 | if (self->length >= length) |
Jeff Thompson | f2349af | 2013-08-08 14:05:37 -0700 | [diff] [blame] | 65 | return NDN_ERROR_success; |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 66 | |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 67 | return ndn_DynamicUInt8Array_reallocArray(self, length); |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 70 | /** |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 71 | * Copy value into self->array at offset, using ndn_DynamicUInt8Array_ensureLength to make sure self->array has enough length. |
| 72 | * @param self pointer to the ndn_DynamicUInt8Array struct |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 73 | * @param value the buffer to copy from |
| 74 | * @param valueLength the length of the value buffer |
| 75 | * @param offset the offset in self->array to copy to |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 76 | * @return 0 for success, else an error code if need to reallocate the array but can't |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 77 | */ |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 78 | static inline ndn_Error ndn_DynamicUInt8Array_set |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 79 | (struct ndn_DynamicUInt8Array *self, uint8_t *value, size_t valueLength, size_t offset) |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 80 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 81 | ndn_Error error; |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 82 | if ((error = ndn_DynamicUInt8Array_ensureLength(self, valueLength + offset))) |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 83 | return error; |
| 84 | ndn_memcpy(self->array + offset, value, valueLength); |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 85 | return NDN_ERROR_success; |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 88 | #ifdef __cplusplus |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 89 | } |
| 90 | #endif |
| 91 | |
| 92 | #endif |
| 93 | |