Rizin
unix-like reverse engineering framework and cli tools
array.h File Reference
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <stdbool.h>
#include "./alloc.h"

Go to the source code of this file.

Macros

#define Array(T)
 
#define array_init(self)    ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
 
#define array_new()    { NULL, 0, 0 }
 
#define array_get(self, index)    (assert((uint32_t)index < (self)->size), &(self)->contents[index])
 
#define array_front(self)   array_get(self, 0)
 
#define array_back(self)   array_get(self, (self)->size - 1)
 
#define array_clear(self)   ((self)->size = 0)
 
#define array_reserve(self, new_capacity)    array__reserve((VoidArray *)(self), array__elem_size(self), new_capacity)
 
#define array_delete(self)   array__delete((VoidArray *)self)
 
#define array_push(self, element)
 
#define array_grow_by(self, count)
 
#define array_push_all(self, other)    array_extend((self), (other)->size, (other)->contents)
 
#define array_extend(self, count, contents)
 
#define array_splice(self, index, old_count, new_count, new_contents)
 
#define array_insert(self, index, element)    array__splice((VoidArray *)(self), array__elem_size(self), index, 0, 1, &element)
 
#define array_erase(self, index)    array__erase((VoidArray *)(self), array__elem_size(self), index)
 
#define array_pop(self)   ((self)->contents[--(self)->size])
 
#define array_assign(self, other)    array__assign((VoidArray *)(self), (const VoidArray *)(other), array__elem_size(self))
 
#define array_swap(self, other)    array__swap((VoidArray *)(self), (VoidArray *)(other))
 
#define array_search_sorted_with(self, compare, needle, index, exists)    array__search_sorted(self, 0, compare, , needle, index, exists)
 
#define array_search_sorted_by(self, field, needle, index, exists)    array__search_sorted(self, 0, _compare_int, field, needle, index, exists)
 
#define array_insert_sorted_with(self, compare, value)
 
#define array_insert_sorted_by(self, field, value)
 
#define array__elem_size(self)   sizeof(*(self)->contents)
 
#define array__search_sorted(self, start, compare, suffix, needle, index, exists)
 
#define _compare_int(a, b)   ((int)*(a) - (int)(b))
 

Functions

typedef Array (void) VoidArray
 
static void array__delete (VoidArray *self)
 
static void array__erase (VoidArray *self, size_t element_size, uint32_t index)
 
static void array__reserve (VoidArray *self, size_t element_size, uint32_t new_capacity)
 
static void array__assign (VoidArray *self, const VoidArray *other, size_t element_size)
 
static void array__swap (VoidArray *self, VoidArray *other)
 
static void array__grow (VoidArray *self, size_t count, size_t element_size)
 
static void array__splice (VoidArray *self, size_t element_size, uint32_t index, uint32_t old_count, uint32_t new_count, const void *elements)
 

Macro Definition Documentation

◆ _compare_int

#define _compare_int (   a,
  b 
)    ((int)*(a) - (int)(b))

Definition at line 241 of file array.h.

◆ Array

#define Array (   T)
Value:
struct { \
T *contents; \
uint32_t size; \
uint32_t capacity; \
}
voidpf void uLong size
Definition: ioapi.h:138

Definition at line 15 of file array.h.

◆ array__elem_size

#define array__elem_size (   self)    sizeof(*(self)->contents)

Definition at line 132 of file array.h.

◆ array__search_sorted

#define array__search_sorted (   self,
  start,
  compare,
  suffix,
  needle,
  index,
  exists 
)
Value:
do { \
*(index) = start; \
*(exists) = false; \
uint32_t size = (self)->size - *(index); \
if (size == 0) break; \
int comparison; \
while (size > 1) { \
uint32_t half_size = size / 2; \
uint32_t mid_index = *(index) + half_size; \
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
if (comparison <= 0) *(index) = mid_index; \
size -= half_size; \
} \
comparison = compare(&((self)->contents[*(index)] suffix), (needle)); \
if (comparison == 0) *(exists) = true; \
else if (comparison < 0) *(index) += 1; \
} while (0)
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
unsigned char suffix[65536]
Definition: gun.c:164
static int compare(const char *s1, const char *s2, int l1, int l2)
Definition: chmd.c:864
while(len< limit &&buf1[len]==buf2[len])++len

Definition at line 220 of file array.h.

◆ array_assign

#define array_assign (   self,
  other 
)     array__assign((VoidArray *)(self), (const VoidArray *)(other), array__elem_size(self))

Definition at line 84 of file array.h.

◆ array_back

#define array_back (   self)    array_get(self, (self)->size - 1)

Definition at line 33 of file array.h.

◆ array_clear

#define array_clear (   self)    ((self)->size = 0)

Definition at line 35 of file array.h.

◆ array_delete

#define array_delete (   self)    array__delete((VoidArray *)self)

Definition at line 41 of file array.h.

◆ array_erase

#define array_erase (   self,
  index 
)     array__erase((VoidArray *)(self), array__elem_size(self), index)

Definition at line 79 of file array.h.

◆ array_extend

#define array_extend (   self,
  count,
  contents 
)
Value:
(VoidArray *)(self), array__elem_size(self), (self)->size, \
0, count, contents \
)
static void array__splice(VoidArray *self, size_t element_size, uint32_t index, uint32_t old_count, uint32_t new_count, const void *elements)
Definition: array.h:183
#define array__elem_size(self)
Definition: array.h:132
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98

Definition at line 59 of file array.h.

◆ array_front

#define array_front (   self)    array_get(self, 0)

Definition at line 31 of file array.h.

◆ array_get

#define array_get (   self,
  index 
)     (assert((uint32_t)index < (self)->size), &(self)->contents[index])

Definition at line 28 of file array.h.

◆ array_grow_by

#define array_grow_by (   self,
  count 
)
Value:
(array__grow((VoidArray *)(self), count, array__elem_size(self)), \
memset((self)->contents + (self)->size, 0, (count) * array__elem_size(self)), \
(self)->size += (count))
static void array__grow(VoidArray *self, size_t count, size_t element_size)
Definition: array.h:173

Definition at line 49 of file array.h.

◆ array_init

#define array_init (   self)     ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)

Definition at line 22 of file array.h.

◆ array_insert

#define array_insert (   self,
  index,
  element 
)     array__splice((VoidArray *)(self), array__elem_size(self), index, 0, 1, &element)

Definition at line 75 of file array.h.

◆ array_insert_sorted_by

#define array_insert_sorted_by (   self,
  field,
  value 
)
Value:
do { \
unsigned index, exists; \
array_search_sorted_by(self, field, (value) field, &index, &exists); \
if (!exists) array_insert(self, index, value); \
} while (0)
#define array_insert(self, index, element)
Definition: array.h:75
static int value
Definition: cmd_api.c:93

Definition at line 121 of file array.h.

◆ array_insert_sorted_with

#define array_insert_sorted_with (   self,
  compare,
  value 
)
Value:
do { \
unsigned index, exists; \
array_search_sorted_with(self, compare, &(value), &index, &exists); \
if (!exists) array_insert(self, index, value); \
} while (0)

Definition at line 110 of file array.h.

◆ array_new

#define array_new ( )     { NULL, 0, 0 }

Definition at line 25 of file array.h.

◆ array_pop

#define array_pop (   self)    ((self)->contents[--(self)->size])

Definition at line 82 of file array.h.

◆ array_push

#define array_push (   self,
  element 
)
Value:
(array__grow((VoidArray *)(self), 1, array__elem_size(self)), \
(self)->contents[(self)->size++] = (element))

Definition at line 43 of file array.h.

◆ array_push_all

#define array_push_all (   self,
  other 
)     array_extend((self), (other)->size, (other)->contents)

Definition at line 54 of file array.h.

◆ array_reserve

#define array_reserve (   self,
  new_capacity 
)     array__reserve((VoidArray *)(self), array__elem_size(self), new_capacity)

Definition at line 37 of file array.h.

◆ array_search_sorted_by

#define array_search_sorted_by (   self,
  field,
  needle,
  index,
  exists 
)     array__search_sorted(self, 0, _compare_int, field, needle, index, exists)

Definition at line 105 of file array.h.

◆ array_search_sorted_with

#define array_search_sorted_with (   self,
  compare,
  needle,
  index,
  exists 
)     array__search_sorted(self, 0, compare, , needle, index, exists)

Definition at line 98 of file array.h.

◆ array_splice

#define array_splice (   self,
  index,
  old_count,
  new_count,
  new_contents 
)
Value:
(VoidArray *)(self), array__elem_size(self), index, \
old_count, new_count, new_contents \
)

Definition at line 68 of file array.h.

◆ array_swap

#define array_swap (   self,
  other 
)     array__swap((VoidArray *)(self), (VoidArray *)(other))

Definition at line 87 of file array.h.

Function Documentation

◆ Array()

typedef Array ( void  )

◆ array__assign()

static void array__assign ( VoidArray *  self,
const VoidArray *  other,
size_t  element_size 
)
inlinestatic

Definition at line 161 of file array.h.

161  {
162  array__reserve(self, element_size, other->size);
163  self->size = other->size;
164  memcpy(self->contents, other->contents, self->size * element_size);
165 }
static void array__reserve(VoidArray *self, size_t element_size, uint32_t new_capacity)
Definition: array.h:150
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))

References array__reserve(), and memcpy().

◆ array__delete()

static void array__delete ( VoidArray *  self)
inlinestatic

Definition at line 134 of file array.h.

134  {
135  ts_free(self->contents);
136  self->contents = NULL;
137  self->size = 0;
138  self->capacity = 0;
139 }
#define ts_free
Definition: alloc.h:30
#define NULL
Definition: cris-opc.c:27

References NULL, and ts_free.

◆ array__erase()

static void array__erase ( VoidArray *  self,
size_t  element_size,
uint32_t  index 
)
inlinestatic

Definition at line 141 of file array.h.

142  {
143  assert(index < self->size);
144  char *contents = (char *)self->contents;
145  memmove(contents + index * element_size, contents + (index + 1) * element_size,
146  (self->size - index - 1) * element_size);
147  self->size--;
148 }
assert(limit<=UINT32_MAX/2)

References assert().

◆ array__grow()

static void array__grow ( VoidArray *  self,
size_t  count,
size_t  element_size 
)
inlinestatic

Definition at line 173 of file array.h.

173  {
174  size_t new_size = self->size + count;
175  if (new_size > self->capacity) {
176  size_t new_capacity = self->capacity * 2;
177  if (new_capacity < 8) new_capacity = 8;
178  if (new_capacity < new_size) new_capacity = new_size;
179  array__reserve(self, element_size, new_capacity);
180  }
181 }

References array__reserve(), and count.

◆ array__reserve()

static void array__reserve ( VoidArray *  self,
size_t  element_size,
uint32_t  new_capacity 
)
inlinestatic

Definition at line 150 of file array.h.

150  {
151  if (new_capacity > self->capacity) {
152  if (self->contents) {
153  self->contents = ts_realloc(self->contents, new_capacity * element_size);
154  } else {
155  self->contents = ts_malloc(new_capacity * element_size);
156  }
157  self->capacity = new_capacity;
158  }
159 }
#define ts_realloc
Definition: alloc.h:27
#define ts_malloc
Definition: alloc.h:21

References ts_malloc, and ts_realloc.

Referenced by array__assign(), array__grow(), and array__splice().

◆ array__splice()

static void array__splice ( VoidArray *  self,
size_t  element_size,
uint32_t  index,
uint32_t  old_count,
uint32_t  new_count,
const void *  elements 
)
inlinestatic

Definition at line 183 of file array.h.

185  {
186  uint32_t new_size = self->size + new_count - old_count;
187  uint32_t old_end = index + old_count;
188  uint32_t new_end = index + new_count;
189  assert(old_end <= self->size);
190 
191  array__reserve(self, element_size, new_size);
192 
193  char *contents = (char *)self->contents;
194  if (self->size > old_end) {
195  memmove(
196  contents + new_end * element_size,
197  contents + old_end * element_size,
198  (self->size - old_end) * element_size
199  );
200  }
201  if (new_count > 0) {
202  if (elements) {
203  memcpy(
204  (contents + index * element_size),
205  elements,
206  new_count * element_size
207  );
208  } else {
209  memset(
210  (contents + index * element_size),
211  0,
212  new_count * element_size
213  );
214  }
215  }
216  self->size += new_count - old_count;
217 }
return memset(p, 0, total)
unsigned int uint32_t
Definition: sftypes.h:29
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4

References array__reserve(), assert(), if(), memcpy(), and memset().

◆ array__swap()

static void array__swap ( VoidArray *  self,
VoidArray *  other 
)
inlinestatic

Definition at line 167 of file array.h.

167  {
168  VoidArray swap = *other;
169  *other = *self;
170  *self = swap;
171 }
#define swap(a, b)
Definition: qsort.h:111

References swap.