1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
/* EINA - EFL data type library
* Copyright (C) 2016 ProFUSION embedded systems
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library;
* if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _EINA_SLICE_H
#define _EINA_SLICE_H
#include "eina_types.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
/**
* @addtogroup Eina_Slice_Group Memory Slices
*
* @brief These functions provide memory slices in read-only and
* read-write forms.
*
* Memory slices define a contiguous linear memory starting at a given
* pointer (@c mem) and spanning for a given length (@c len).
*
* They may be read-only (Eina_Slice) or read-write (Eina_Rw_Slice).
*
* @since 1.19
*/
/**
* @addtogroup Eina_Data_Types_Group Data Types
*
* @{
*/
/**
* @defgroup Eina_Slice_Group Memory Slices
*
* @{
*/
/**
* @typedef Eina_Slice
* Defines a read-only memory region.
*
* The slice is a memory starting at @c mem and accessible up to @c
* len bytes.
*
* @see Eina_Rw_Slice for read-write memory regions.
*
* @since 1.19
*/
typedef struct _Eina_Slice Eina_Slice;
/**
* @typedef Eina_Rw_Slice
* Defines a read-and-write able memory region.
*
* The slice is a memory starting at @c mem and accessible up to @c
* len bytes.
*
* @see Eina_Slice for read-only memory regions.
*
* @since 1.19
*/
typedef struct _Eina_Rw_Slice Eina_Rw_Slice;
/**
* @struct _Eina_Slice
* Defines a read-only memory region.
*
* The slice is a memory starting at @c mem and accessible up to @c
* len bytes.
*
* @see Eina_Rw_Slice for read-write memory regions.
*
* @since 1.19
*/
struct _Eina_Slice
{
size_t len; /**< size of memory pointed by @c mem */
union {
const void *mem; /**< memory pointed by this slice. Just read, never modify it. */
const uint8_t *bytes; /**< memory as uint8_t pointer */
};
};
/**
* @struct _Eina_Rw_Slice
* Defines a read-and-write able memory region.
*
* The slice is a memory starting at @c mem and accessible up to @c
* len bytes.
*
* @see Eina_Slice for read-only memory regions.
*
* @since 1.19
*/
struct _Eina_Rw_Slice
{
size_t len; /**< size of memory pointed by @c mem */
union {
void *mem; /**< memory pointed by this slice. It's write able. */
uint8_t *bytes; /**< memory as uint8_t pointer */
};
};
/**
* @brief Convert the Read-write slice to read-only.
*
* @param rw_slice the read-write slice to convert.
* @return the red-only slice matching the slice.
*/
static inline Eina_Slice eina_rw_slice_slice_get(const Eina_Rw_Slice rw_slice);
/**
* @brief Creates a duplicate of slice's memory.
*
* @param slice the input to duplicate
* @return a new read-write slice with new @c mem that matches @a slice
* contents. The new @c mem is allocated with malloc() and must
* be released with free().
*
* @see eina_rw_slice_copy()
* @see eina_rw_slice_dup()
*
* @since 1.19
*/
static inline Eina_Rw_Slice eina_slice_dup(const Eina_Slice slice) EINA_WARN_UNUSED_RESULT;
/**
* @brief Creates a duplicate of slice's memory.
*
* @param rw_slice the input to duplicate
* @return a new read-write slice with new @c mem that matches @a slice
* contents. The new @c mem is allocated with malloc() and must
* be released with free().
*
* @see eina_rw_slice_copy()
* @see eina_slice_dup()
*
* @since 1.19
*/
static inline Eina_Rw_Slice eina_rw_slice_dup(const Eina_Rw_Slice rw_slice) EINA_WARN_UNUSED_RESULT;
/**
* @brief Compare two slices, similar to memcmp()
*
* @param a the first slice to compare.
* @param b the second slice to compare.
* @return 0 if equal, < 0 if a < b, > 0 if a > b
*
* @since 1.19
*/
static inline int eina_slice_compare(const Eina_Slice a, const Eina_Slice b);
/**
* @brief Compare two slices, similar to memcmp()
*
* @param a the first slice to compare.
* @param b the second slice to compare.
* @return 0 if equal, < 0 if a < b, > 0 if a > b
*
* @since 1.19
*/
static inline int eina_rw_slice_compare(const Eina_Rw_Slice a, const Eina_Rw_Slice b);
/**
* @brief Copy a read-only slice to a read-write one, similar to memcpy().
*
* @param dest where to write the memory.
* @param src where to load memory.
*
* @return a new slice with the resulting write. Note that the length
* (@c len) will be the smallest of @a dest and @a src.
*
* @see eina_rw_slice_dup()
* @see eina_slice_dup()
*
* @since 1.19
*/
static inline Eina_Rw_Slice eina_rw_slice_copy(const Eina_Rw_Slice dest, const Eina_Slice src);
/**
* @brief Seek within a slice, similar to fseek().
*
* @param slice the containing slice to seek inside.
* @param offset how to get to the new position.
* @param whence SEEK_SET, SEEK_END as fseek().
* @return a new slice contained inside, it will start at the given
* offset and have a length that goes until the end of the @a
* slice. If an invalid @a whence, a zero-sized slice starting
* at @a slice mem will be returned. The slice is guaranteed
* to be contained within @a slice, even if offset causes it
* to go out of bounds, then it will be clamped to 0 and
* slice.len.
*
* @since 1.19
*/
static inline Eina_Slice eina_slice_seek(const Eina_Slice slice, ssize_t offset, int whence);
/**
* @brief Seek within a read-write slice, similar to fseek().
*
* @param rw_slice the containing slice to seek inside.
* @param offset how to get to the new position.
* @param whence SEEK_SET, SEEK_END as fseek().
* @return a new slice contained inside, it will start at the given
* offset and have a length that goes until the end of the @a
* rw_slice. If an invalid @a whence, a zero-sized slice
* starting at @a rw_slice mem will be returned. The slice is
* guaranteed to be contained within @a rw_slice, even if
* offset causes it to go out of bounds, then it will be
* clamped to 0 and slice.len.
*
* @since 1.19
*/
static inline Eina_Rw_Slice eina_rw_slice_seek(const Eina_Rw_Slice rw_slice, ssize_t offset, int whence);
/**
* @brief Find a character inside the slice, similar to memchr().
*
* @param slice the reference memory.
* @param c the byte (character) to find.
* @return the memory within slice or @c NULL if not found.
*
* @since 1.19
*/
static inline const void *eina_slice_strchr(const Eina_Slice slice, int c);
/**
* @brief Find a needle inside the slice, similar to memmem().
*
* @param slice the reference memory.
* @param needle what to find.
* @return the memory within slice or @c NULL if not found.
*
* @since 1.19
*/
static inline const void *eina_slice_find(const Eina_Slice slice, const Eina_Slice needle);
/**
* @brief Checks if the slice starts with a prefix.
*
* @param slice the reference memory.
* @param prefix the slice to check if @a slice starts with.
* @return #EINA_TRUE if @a slice starts with @a prefix, #EINA_FALSE otherwise.
*
* @since 1.19
*/
static inline Eina_Bool eina_slice_startswith(const Eina_Slice slice, const Eina_Slice prefix);
/**
* @brief Checks if the slice ends with a suffix.
*
* @param slice the reference memory.
* @param suffix the slice to check if @a slice ends with.
* @return #EINA_TRUE if @a slice ends with @a suffix, #EINA_FALSE otherwise.
*
* @since 1.19
*/
static inline Eina_Bool eina_slice_endswith(const Eina_Slice slice, const Eina_Slice suffix);
/**
* @brief Find a character inside the slice, similar to memchr().
*
* @param rw_slice the reference memory.
* @param c the byte (character) to find.
* @return the memory within slice or @c NULL if not found.
*
* @since 1.19
*/
static inline void *eina_rw_slice_strchr(const Eina_Rw_Slice rw_slice, int c);
/**
* @brief Find a needle inside the slice, similar to memmem().
*
* @param rw_slice the reference memory.
* @param needle what to find.
* @return the memory within slice or @c NULL if not found.
*
* @since 1.19
*/
static inline void *eina_rw_slice_find(const Eina_Rw_Slice rw_slice, const Eina_Slice needle);
/**
* @brief Checks if the slice starts with a prefix.
*
* @param slice the reference memory.
* @param prefix the slice to check if @a slice starts with.
* @return #EINA_TRUE if @a slice starts with @a prefix, #EINA_FALSE otherwise.
*
* @since 1.19
*/
static inline Eina_Bool eina_rw_slice_startswith(const Eina_Rw_Slice slice, const Eina_Slice prefix);
/**
* @brief Checks if the slice ends with a suffix.
*
* @param slice the reference memory.
* @param suffix the slice to check if @a slice ends with.
* @return #EINA_TRUE if @a slice ends with @a suffix, #EINA_FALSE otherwise.
*
* @since 1.19
*/
static inline Eina_Bool eina_rw_slice_endswith(const Eina_Rw_Slice slice, const Eina_Slice suffix);
/**
* @brief The memory position where the slice ends.
*
* @note this is out-of the slice, the first byte after it ends and
* must not be accessed.
*
* @param slice the reference memory.
* @return the first byte after the slice ends.
*
* @since 1.19
*/
static inline const void *eina_slice_end_get(const Eina_Slice slice);
/**
* @brief The memory position where the slice ends.
*
* @note this is out-of the slice, the first byte after it ends and
* must not be accessed.
*
* @param rw_slice the reference memory.
* @return the first byte after the slice ends.
*
* @since 1.19
*/
static inline void *eina_rw_slice_end_get(const Eina_Rw_Slice rw_slice);
/**
* @brief A null-terminated string for this slice.
*
* @param slice the reference memory.
* @return newly allocated memory or @c NULL on error
*
* @since 1.19
*/
static inline char *eina_slice_strdup(const Eina_Slice slice);
/**
* @brief A null-terminated string for this slice.
*
* @param slice the reference memory.
* @return newly allocated memory or @c NULL on error
*
* @since 1.19
*/
static inline char *eina_rw_slice_strdup(const Eina_Rw_Slice rw_slice);
/**
* @def EINA_SLICE_ARRAY(buf)
*
* Initializer for arrays of any kind.
*
* It is often useful for globals.
*
* @note This macro is usable with both Eina_Slice or Eina_Rw_Slice.
*
* @code
* static uint8_t buf[1024];
* static Eina_Slice rw_slice = EINA_SLICE_ARRAY(buf);
* @endcode
*
* @see EINA_SLICE_STR_LITERAL() for specific version that checks for string literals.
*
* @since 1.19
*/
#ifdef __cplusplus
#define EINA_SLICE_ARRAY(buf) {((sizeof(buf) / sizeof((buf)[0])) * sizeof((buf)[0])), (buf)}
#else
#define EINA_SLICE_ARRAY(buf) {.len = ((sizeof(buf) / sizeof((buf)[0])) * sizeof((buf)[0])), .mem = (buf)}
#endif
/**
* @def EINA_RW_SLICE_DECLARE(name, length)
*
* Declare a local (stack) array for storage at given @a length and
* initialize an Eina_Rw_Slice called @a name.
*
* @param name the name of the variable to be the Eina_Rw_Slice
* @param length the size in bytes of the storage.
*
* @since 1.19
*/
#define EINA_RW_SLICE_DECLARE(name, length) \
uint8_t _eina_slice_storage_ ## name [(length)] = { 0 }; \
Eina_Rw_Slice name = EINA_SLICE_ARRAY(_eina_slice_storage_ ## name)
/**
* @def EINA_SLICE_STR_LITERAL(buf)
*
* Initializer for string literals (those declared as
* double-quoted). The size will @b NOT include the trainling
* null-terminator.
*
* It is often useful for globals.
*
* @note This macro is usable with both Eina_Slice or Eina_Rw_Slice.
*
* @code
* static const Eina_Slice ro_slice = EINA_SLICE_STR_LITERAL("hello world");
* @endcode
*
* @see EINA_SLICE_STR() for more generic version.
* @see EINA_SLICE_ARRAY() for version that uses a general array.
*
* @since 1.19
*/
#ifdef __cplusplus
#define EINA_SLICE_STR_LITERAL(buf) {(sizeof("" buf) - 1), (buf)}
#else
#define EINA_SLICE_STR_LITERAL(buf) {.len = (sizeof("" buf) - 1), .mem = (buf)}
#endif
/**
* @def EINA_SLICE_STR(str)
*
* Initializer for strings (uses strlen()).
*
* @note This macro is usable with both Eina_Slice or Eina_Rw_Slice.
*
* @code
* Eina_Slice ro_slice = EINA_SLICE_STR("hello world");
* @endcode
*
* @see EINA_SLICE_STR() for specific version using literals.
*
* @since 1.19
*/
#ifdef __cplusplus
#define EINA_SLICE_STR(str) {strlen((str)), (str)}
#else
#define EINA_SLICE_STR(str) {.len = strlen((str)), .mem = (str)}
#endif
/**
* @def EINA_SLICE_STR_FMT
*
* To be used in printf()-like statements, prints the slice as a
* string, its @c len is to be used, then it doesn need the null
* terminator.
*
* Use with EINA_SLICE_STR_PRINT()
*
* @note This macro is usable with both Eina_Slice or Eina_Rw_Slice.
*
* @code
* Eina_Slice s = EINA_SLICE_STR_LITERAL("hello");
* printf("s=" EINA_SLICE_STR_FMT "\n", EINA_SLICE_STR_PRINT(s));
* @endcode
*
* @since 1.19
*/
#define EINA_SLICE_STR_FMT "%.*s"
/**
* @def EINA_SLICE_STR_PRINT(s)
*
* To be used in printf()-like statements when EINA_SLICE_STR_FMT was
* used, it will print the slice as a string up to @c len.
*
* Use with EINA_SLICE_STR_FMT.
*
* @note This macro is usable with both Eina_Slice or Eina_Rw_Slice.
*
* @code
* Eina_Slice s = EINA_SLICE_STR_LITERAL("hello");
* printf("s=" EINA_SLICE_STR_FMT "\n", EINA_SLICE_STR_PRINT(s));
* @endcode
*
* @since 1.19
*/
#define EINA_SLICE_STR_PRINT(s) (int)(s).len, (const char *)(s).mem
/**
* @def EINA_SLICE_FMT
*
* To be used in printf()-like statements, prints the slice as
* @c 0x1234+12 (@c mem + @c len).
*
* Use with EINA_SLICE_PRINT()
*
* @note This macro is usable with both Eina_Slice or Eina_Rw_Slice.
*
* @code
* Eina_Slice s = EINA_SLICE_STR_LITERAL("hello");
* printf("s=" EINA_SLICE_FMT "\n", EINA_SLICE_PRINT(s));
* @endcode
*
* @since 1.19
*/
#define EINA_SLICE_FMT "%p+%zu"
/**
* @def EINA_SLICE_PRINT(s)
*
* To be used in printf()-like statements when EINA_SLICE_FMT was
* used, it will print the slice @c mem and @c len.
*
* Use with EINA_SLICE_FMT.
*
* @note This macro is usable with both Eina_Slice or Eina_Rw_Slice.
*
* @param s the slice
*
* @code
* Eina_Slice s = EINA_SLICE_STR_LITERAL("hello");
* printf("s=" EINA_SLICE_FMT "\n", EINA_SLICE_PRINT(s));
* @endcode
*
* @since 1.19
*/
#define EINA_SLICE_PRINT(s) (s).mem, (s).len
/**
* @def EINA_SLICE_FOREACH(s, itr)
*
* Iterate over the slice memory, using @c itr. Each increment will be
* using the size of @c itr pointer (int32_t* will do in increments of
* 4 bytes).
*
* @note This macro is usable with both Eina_Slice or Eina_Rw_Slice.
*
* @note Be aware of memory alignment! Accessing unaligned memory may
* not be supported in some architectures.
*
* @param s the slice
* @param itr the iterator to hold each byte. Use a proper type, not
* "void*" or "const void*" as it doesn't have an intrinsic
* size.
*
* @since 1.19
*/
#ifdef __cplusplus
#define EINA_SLICE_FOREACH(s, itr) \
for ((itr) = static_cast<__typeof__(itr)>((s).mem); \
(itr) < static_cast<__typeof__(itr)>(static_cast<void *>((s).bytes + (s).len)); \
(itr)++)
#else
#define EINA_SLICE_FOREACH(s, itr) \
for ((itr) = (s).mem; \
(void *)(itr) < (void *)((s).bytes + (s).len); \
(itr)++)
#endif
#include "eina_inline_slice.x"
/**
* @}
*/
/**
* @}
*/
#endif /* _EINA_SLICE_H */
|