2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to you under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14
* implied. See the License for the specific language governing
15
* permissions and limitations under the License.
18
#ifndef AVRO_ALLOCATION_H
19
#define AVRO_ALLOCATION_H
22
#define CLOSE_EXTERN }
30
* Allocation interface. You can provide a custom allocator for the
31
* library, should you wish. The allocator is provided as a single
32
* generic function, which can emulate the standard malloc, realloc, and
33
* free functions. The design of this allocation interface is inspired
34
* by the implementation of the Lua interpreter.
36
* The ptr parameter will be the location of any existing memory
37
* buffer. The osize parameter will be the size of this existing
38
* buffer. If ptr is NULL, then osize will be 0. The nsize parameter
39
* will be the size of the new buffer, or 0 if the new buffer should be
42
* If nsize is 0, then the allocation function must return NULL. If
43
* nsize is not 0, then it should return NULL if the allocation fails.
47
(*avro_allocator_t)(void *user_data, void *ptr, size_t osize, size_t nsize);
49
void avro_set_allocator(avro_allocator_t alloc, void *user_data);
51
struct avro_allocator_state {
52
avro_allocator_t alloc;
56
extern struct avro_allocator_state AVRO_CURRENT_ALLOCATOR;
58
#define avro_realloc(ptr, osz, nsz) \
59
(AVRO_CURRENT_ALLOCATOR.alloc \
60
(AVRO_CURRENT_ALLOCATOR.user_data, \
63
#define avro_malloc(sz) (avro_realloc(NULL, 0, (sz)))
64
#define avro_free(ptr, osz) (avro_realloc((ptr), (osz), 0))
66
#define avro_new(type) (avro_realloc(NULL, 0, sizeof(type)))
67
#define avro_freet(type, ptr) (avro_realloc((ptr), sizeof(type), 0))
69
void *avro_calloc(size_t count, size_t size);
72
* This is probably too clever for our own good, but when we duplicate a
73
* string, we actually store its size in the same allocated memory
74
* buffer. That lets us free the string later, without having to call
75
* strlen to get its size, and without the containing struct having to
76
* manually store the strings length.
78
* This means that any string return by avro_strdup MUST be freed using
79
* avro_str_free, and the only thing that can be passed into
80
* avro_str_free is a string created via avro_strdup.
83
char *avro_str_alloc(size_t str_size);
84
char *avro_strdup(const char *str);
85
void avro_str_free(char *str);