php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
fpm_arrays.h
Go to the documentation of this file.
1 /* (c) 2007,2008 Andrei Nigmatulin */
2
3#ifndef FPM_ARRAYS_H
4#define FPM_ARRAYS_H 1
5
6#include <stdlib.h>
7#include <string.h>
8
9#include "php.h"
10
12 void *data;
13 size_t sz;
14 size_t used;
15 size_t allocated;
16};
17
18static inline struct fpm_array_s *fpm_array_init(struct fpm_array_s *a, unsigned int sz, unsigned int initial_num) /* {{{ */
19{
20 void *allocated = 0;
21
22 if (!a) {
23 a = malloc(sizeof(struct fpm_array_s));
24
25 if (!a) {
26 return 0;
27 }
28
29 allocated = a;
30 }
31
32 a->sz = sz;
33
34 a->data = calloc(sz, initial_num);
35
36 if (!a->data) {
37 free(allocated);
38 return 0;
39 }
40
41 a->allocated = initial_num;
42 a->used = 0;
43
44 return a;
45}
46/* }}} */
47
48static inline void *fpm_array_item(struct fpm_array_s *a, unsigned int n) /* {{{ */
49{
50 char *ret;
51
52 ret = (char *) a->data + a->sz * n;
53
54 return ret;
55}
56/* }}} */
57
58static inline void *fpm_array_item_last(struct fpm_array_s *a) /* {{{ */
59{
60 return fpm_array_item(a, a->used - 1);
61}
62/* }}} */
63
64static inline int fpm_array_item_remove(struct fpm_array_s *a, unsigned int n) /* {{{ */
65{
66 int ret = -1;
67
68 if (n < a->used - 1) {
69 void *last = fpm_array_item(a, a->used - 1);
70 void *to_remove = fpm_array_item(a, n);
71
72 memcpy(to_remove, last, a->sz);
73
74 ret = n;
75 }
76
77 --a->used;
78
79 return ret;
80}
81/* }}} */
82
83static inline void *fpm_array_push(struct fpm_array_s *a) /* {{{ */
84{
85 void *ret;
86
87 if (a->used == a->allocated) {
88 size_t new_allocated = a->allocated ? a->allocated * 2 : 20;
89 void *new_ptr = safe_perealloc(a->data, a->sz, new_allocated, 0, true);
90
91 if (!new_ptr) {
92 return 0;
93 }
94
95 a->data = new_ptr;
96 a->allocated = new_allocated;
97 }
98
99 ret = fpm_array_item(a, a->used);
100
101 ++a->used;
102
103 return ret;
104}
105/* }}} */
106
107static inline void fpm_array_free(struct fpm_array_s *a) /* {{{ */
108{
109 free(a->data);
110 a->data = 0;
111 a->sz = 0;
112 a->used = a->allocated = 0;
113}
114/* }}} */
115
116#endif
zend_long n
Definition ffi.c:4979
memcpy(ptr1, ptr2, size)
size_t used
Definition fpm_arrays.h:14
size_t allocated
Definition fpm_arrays.h:15
void * data
Definition fpm_arrays.h:12
size_t sz
Definition fpm_arrays.h:13
$obj a
Definition test.php:84
#define safe_perealloc(ptr, nmemb, size, offset, persistent)
Definition zend_alloc.h:203
int last
zval * ret