2 * Copyright 2020 EfficiOS, Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include "common/align.h"
14 #define INIT_ALLOC_SIZE 4
16 static inline int get_count_order(unsigned int count
)
20 order
= lttng_fls(count
) - 1;
21 if (count
& (count
- 1))
27 int bytecode_init(struct lttng_filter_bytecode_alloc
**fb
)
31 alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + INIT_ALLOC_SIZE
;
32 *fb
= calloc(alloc_len
, 1);
36 (*fb
)->alloc_len
= alloc_len
;
42 int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc
**fb
, uint32_t align
, uint32_t len
)
45 uint32_t padding
= offset_align((*fb
)->b
.len
, align
);
46 uint32_t new_len
= (*fb
)->b
.len
+ padding
+ len
;
47 uint32_t new_alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + new_len
;
48 uint32_t old_alloc_len
= (*fb
)->alloc_len
;
50 if (new_len
> LTTNG_FILTER_MAX_LEN
)
53 if (new_alloc_len
> old_alloc_len
) {
54 struct lttng_filter_bytecode_alloc
*newptr
;
57 max_t(uint32_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
58 newptr
= realloc(*fb
, new_alloc_len
);
62 /* We zero directly the memory from start of allocation. */
63 memset(&((char *) *fb
)[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
64 (*fb
)->alloc_len
= new_alloc_len
;
66 (*fb
)->b
.len
+= padding
;
73 int bytecode_push(struct lttng_filter_bytecode_alloc
**fb
, const void *data
,
74 uint32_t align
, uint32_t len
)
78 offset
= bytecode_reserve(fb
, align
, len
);
81 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
86 int bytecode_push_logical(struct lttng_filter_bytecode_alloc
**fb
,
87 struct logical_op
*data
,
88 uint32_t align
, uint32_t len
,
89 uint16_t *skip_offset
)
93 offset
= bytecode_reserve(fb
, align
, len
);
96 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
98 (void *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
99 - (void *) &(*fb
)->b
.data
[0];
104 * Allocate an lttng_bytecode object and copy the given original bytecode.
106 * Return allocated bytecode or NULL on error.
109 struct lttng_filter_bytecode
*lttng_filter_bytecode_copy(
110 const struct lttng_filter_bytecode
*orig_f
)
112 struct lttng_filter_bytecode
*bytecode
= NULL
;
114 bytecode
= zmalloc(sizeof(*bytecode
) + orig_f
->len
);
119 memcpy(bytecode
, orig_f
, sizeof(*bytecode
) + orig_f
->len
);