void lru_free_items_impl(struct lru *);
struct lru *lru_create_impl(unsigned int, knot_mm_t *, knot_mm_t *);
void *lru_get_impl(struct lru *, const char *, unsigned int, unsigned int, _Bool, _Bool *);
+void *mm_realloc(knot_mm_t *, void *, size_t, size_t);
knot_rrset_t *kr_ta_get(map_t *, const knot_dname_t *);
int kr_ta_add(map_t *, const knot_dname_t *, uint16_t, uint32_t, const uint8_t *, uint16_t);
int kr_ta_del(map_t *, const knot_dname_t *);
lru_free_items_impl
lru_create_impl
lru_get_impl
+ mm_realloc
# Trust anchors
kr_ta_get
kr_ta_add
if ret ~= 0 then return nil, knot_error_t(ret) end
return true
end,
+ -- Resize packet wire to a new size
+ resize = function (pkt, new_size)
+ assert(ffi.istype(knot_pkt_t, pkt))
+ local ptr = C.mm_realloc(pkt.mm, pkt.wire, new_size, pkt.max_size)
+ if ptr == nil then return end
+ pkt.wire = ptr
+ pkt.max_size = new_size
+ return true
+ end,
},
})
-- Metatype for query
static bool isaac_seeded = false;
#define SEED_SIZE 256
+void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size)
+{
+ if (mm) {
+ void *p = mm->alloc(mm->ctx, size);
+ if (p == NULL) {
+ return NULL;
+ } else {
+ if (what) {
+ memcpy(p, what,
+ prev_size < size ? prev_size : size);
+ }
+ mm_free(mm, what);
+ return p;
+ }
+ } else {
+ return realloc(what, size);
+ }
+}
void *mm_malloc(void *ctx, size_t n)
{
}
else free(what);
}
-static inline void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size)
-{
- if (mm) {
- void *p = mm->alloc(mm->ctx, size);
- if (p == NULL) {
- return NULL;
- } else {
- if (what) {
- memcpy(p, what,
- prev_size < size ? prev_size : size);
- }
- mm_free(mm, what);
- return p;
- }
- } else {
- return realloc(what, size);
- }
-}
+
+/** Realloc implementation using memory context. */
+KR_EXPORT
+void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size);
/** Trivial malloc() wrapper. */
void *mm_malloc(void *ctx, size_t n);
-- Test manipulating sections
ok(pkt:begin(kres.section.ANSWER), 'switching sections works')
local res, err = pkt:put(nil, 0, 0, 0, '')
- isnt(res, 'inserting nil entry doesnt work')
+ isnt(res, true, 'inserting nil entry doesnt work')
isnt(err.code, 0, 'error code is non-zero')
isnt(tostring(res), '', 'inserting nil returns invalid parameter')
ok(pkt:put(pkt:qname(), 900, pkt:qclass(), kres.type.A, '\1\2\3\4'), 'adding rrsets works')
same(parsed:tostring(), pkt:tostring(), 'parsed packet is equal to source packet')
-- Test adding RR sets directly
- local copy = kres.packet(512)
+ local copy = kres.packet(23)
copy:question(todname('hello'), kres.class.IN, kres.type.A)
copy:begin(kres.section.ANSWER)
local rr = kres.rrset(pkt:qname(), kres.type.A, kres.class.IN, 66)
rr:add_rdata('\4\3\2\1', 4)
+ ok(not copy:put_rr(rr), 'adding RR sets checks for available space')
+ ok(copy:resize(512), 'resizing packet works')
ok(copy:put_rr(rr), 'adding RR sets directly works')
- ok(copy:recycle())
+ ok(copy:recycle(), 'recycling packet works')
-- Test recycling of packets
-- Clear_payload keeps header + question intact