45
77
#define BSTR(buf) ((char *)BPTR(buf))
46
78
#define BCAP(buf) (buf_forward_capacity (buf))
80
void buf_clear (struct buffer *buf);
82
struct buffer clear_buf (void);
83
void free_buf (struct buffer *buf);
85
bool buf_assign (struct buffer *dest, const struct buffer *src);
89
/* for dmalloc debugging */
93
#define alloc_buf(size) alloc_buf_debug (size, __FILE__, __LINE__)
94
#define alloc_buf_gc(size, gc) alloc_buf_gc_debug (size, gc, __FILE__, __LINE__);
95
#define clone_buf(buf) clone_buf_debug (buf, __FILE__, __LINE__);
96
#define gc_malloc(size, clear, arena) gc_malloc_debug (size, clear, arena, __FILE__, __LINE__)
97
#define string_alloc(str, gc) string_alloc_debug (str, gc, __FILE__, __LINE__)
98
#define string_alloc_buf(str, gc) string_alloc_buf_debug (str, gc, __FILE__, __LINE__)
100
struct buffer alloc_buf_debug (size_t size, const char *file, int line);
101
struct buffer alloc_buf_gc_debug (size_t size, struct gc_arena *gc, const char *file, int line);
102
struct buffer clone_buf_debug (const struct buffer* buf, const char *file, int line);
103
void *gc_malloc_debug (size_t size, bool clear, struct gc_arena *a, const char *file, int line);
104
char *string_alloc_debug (const char *str, struct gc_arena *gc, const char *file, int line);
105
struct buffer string_alloc_buf_debug (const char *str, struct gc_arena *gc, const char *file, int line);
48
109
struct buffer alloc_buf (size_t size);
110
struct buffer alloc_buf_gc (size_t size, struct gc_arena *gc); /* allocate buffer with garbage collection */
49
111
struct buffer clone_buf (const struct buffer* buf);
50
struct buffer alloc_buf_gc (size_t size); /* allocate buffer with garbage collection */
51
struct buffer clear_buf (void);
52
void free_buf (struct buffer *buf);
112
void *gc_malloc (size_t size, bool clear, struct gc_arena *a);
113
char *string_alloc (const char *str, struct gc_arena *gc);
114
struct buffer string_alloc_buf (const char *str, struct gc_arena *gc);
118
#ifdef BUF_INIT_TRACKING
119
#define buf_init(buf, offset) buf_init_debug (buf, offset, __FILE__, __LINE__)
120
bool buf_init_debug (struct buffer *buf, int offset, const char *file, int line);
122
#define buf_init(buf, offset) buf_init_dowork (buf, offset)
54
126
/* inline functions */
129
buf_reset (struct buffer *buf)
56
137
static inline bool
57
buf_init (struct buffer *buf, int offset)
138
buf_init_dowork (struct buffer *buf, int offset)
59
140
if (offset < 0 || offset > buf->capacity || buf->data == NULL)
159
241
void convert_to_one_line (struct buffer *buf);
244
* Parse a string based on a given delimiter char
246
bool buf_parse (struct buffer *buf, const int delim, char *line, const int size);
163
249
* Hex dump -- Output a binary buffer to a hex string and return it.
166
252
format_hex_ex (const uint8_t *data, int size, int maxoutput,
167
int space_break, const char* separator);
253
int space_break, const char* separator,
254
struct gc_arena *gc);
169
256
static inline char *
170
format_hex (const uint8_t *data, int size, int maxoutput)
257
format_hex (const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
172
return format_hex_ex(data, size, maxoutput, 4, " ");
259
return format_hex_ex (data, size, maxoutput, 4, " ", gc);
552
* Classify and mutate strings based on character types.
555
/*#define CHARACTER_CLASS_DEBUG*/
557
/* character classes */
559
#define CC_ANY (1<<0)
560
#define CC_NULL (1<<1)
562
#define CC_ALNUM (1<<2)
563
#define CC_ALPHA (1<<3)
564
#define CC_ASCII (1<<4)
565
#define CC_CNTRL (1<<5)
566
#define CC_DIGIT (1<<6)
567
#define CC_PRINT (1<<7)
568
#define CC_PUNCT (1<<8)
569
#define CC_SPACE (1<<9)
570
#define CC_XDIGIT (1<<10)
572
#define CC_BLANK (1<<11)
573
#define CC_NEWLINE (1<<12)
574
#define CC_CR (1<<13)
576
#define CC_BACKSLASH (1<<14)
577
#define CC_UNDERBAR (1<<15)
578
#define CC_DASH (1<<16)
579
#define CC_DOT (1<<17)
580
#define CC_COMMA (1<<18)
581
#define CC_COLON (1<<19)
582
#define CC_SLASH (1<<20)
583
#define CC_SINGLE_QUOTE (1<<21)
584
#define CC_DOUBLE_QUOTE (1<<22)
585
#define CC_REVERSE_QUOTE (1<<23)
586
#define CC_AT (1<<24)
587
#define CC_EQUAL (1<<25)
590
#define CC_NAME (CC_ALNUM|CC_UNDERBAR)
591
#define CC_CRLF (CC_CR|CC_NEWLINE)
593
bool char_class (const char c, const unsigned int flags);
594
bool string_class (const char *str, const unsigned int inclusive, const unsigned int exclusive);
595
bool string_mod (char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace);
597
const char *string_mod_const (const char *str,
598
const unsigned int inclusive,
599
const unsigned int exclusive,
601
struct gc_arena *gc);
603
#ifdef CHARACTER_CLASS_DEBUG
604
void character_class_debug (void);
608
* Verify that a pointer is correctly aligned
610
#ifdef VERIFY_ALIGNMENT
611
void valign4 (const struct buffer *buf, const char *file, const int line);
612
# define verify_align_4(ptr) valign4(buf, __FILE__, __LINE__)
614
# define verify_align_4(ptr)
445
618
* Very basic garbage collection, mostly for routines that return
446
619
* char ptrs to malloced strings.
451
struct gc_entry *back;
459
struct gc_entry *gc_stack;
462
extern struct gc_thread x_gc_thread[N_THREADS];
464
void *gc_malloc (size_t size);
467
x_gc_free (void *p) {
472
gc_collect (int level)
475
struct gc_thread* thread = &x_gc_thread[thread_number()];
477
while ((e = thread->gc_stack))
479
if (e->level < level)
481
/*printf("GC FREE " ptr_format " lev=%d\n", e, e->level); */
483
thread->gc_stack = e->back;
491
struct gc_thread* thread = &x_gc_thread[thread_number()];
492
return ++thread->gc_level;
496
gc_free_level (int level)
498
struct gc_thread* thread = &x_gc_thread[thread_number()];
501
thread->gc_level = level - 1;
505
#define GCCC debug_gc_check_corrupt(__FILE__, __LINE__)
506
void debug_gc_check_corrupt (const char *file, int line);
622
void x_gc_free (struct gc_arena *a);
625
gc_init (struct gc_arena *a)
631
gc_detach (struct gc_arena *a)
636
static inline struct gc_arena
645
gc_free (struct gc_arena *a)
652
gc_reset (struct gc_arena *a)
658
* Allocate memory to hold a structure
661
void out_of_memory (void);
663
#define ALLOC_OBJ(dptr, type) \
665
check_malloc_return ((dptr) = (type *) malloc (sizeof (type))); \
668
#define ALLOC_OBJ_CLEAR(dptr, type) \
670
ALLOC_OBJ (dptr, type); \
671
memset ((dptr), 0, sizeof(type)); \
674
#define ALLOC_ARRAY(dptr, type, n) \
676
check_malloc_return ((dptr) = (type *) malloc (sizeof (type) * (n))); \
679
#define ALLOC_ARRAY_GC(dptr, type, n, gc) \
681
(dptr) = (type *) gc_malloc (sizeof (type) * (n), false, (gc)); \
684
#define ALLOC_ARRAY_CLEAR(dptr, type, n) \
686
ALLOC_ARRAY (dptr, type, n); \
687
memset ((dptr), 0, (sizeof(type) * (n))); \
690
#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \
692
(dptr) = (type *) gc_malloc (sizeof (type) * (n), true, (gc)); \
695
#define ALLOC_OBJ_GC(dptr, type, gc) \
697
(dptr) = (type *) gc_malloc (sizeof (type), false, (gc)); \
700
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc) \
702
(dptr) = (type *) gc_malloc (sizeof (type), true, (gc)); \
706
check_malloc_return (void *p)
708
void out_of_memory (void);
509
713
#endif /* BUFFER_H */