~stub/ubuntu/trusty/avro-c/trunk

« back to all changes in this revision

Viewing changes to src/avro/allocation.h

  • Committer: Stuart Bishop
  • Date: 2015-05-14 11:53:53 UTC
  • Revision ID: stuart@stuartbishop.net-20150514115353-0cvnrcyohcq5l7yj
Tags: upstream-1.7.7
ImportĀ upstreamĀ versionĀ 1.7.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
8
 * 
 
9
 * http://www.apache.org/licenses/LICENSE-2.0
 
10
 * 
 
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. 
 
16
 */
 
17
 
 
18
#ifndef AVRO_ALLOCATION_H
 
19
#define AVRO_ALLOCATION_H
 
20
#ifdef __cplusplus
 
21
extern "C" {
 
22
#define CLOSE_EXTERN }
 
23
#else
 
24
#define CLOSE_EXTERN
 
25
#endif
 
26
 
 
27
#include <stdlib.h>
 
28
 
 
29
/*
 
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.
 
35
 *
 
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
 
40
 * freed.
 
41
 *
 
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.
 
44
 */
 
45
 
 
46
typedef void *
 
47
(*avro_allocator_t)(void *user_data, void *ptr, size_t osize, size_t nsize);
 
48
 
 
49
void avro_set_allocator(avro_allocator_t alloc, void *user_data);
 
50
 
 
51
struct avro_allocator_state {
 
52
        avro_allocator_t  alloc;
 
53
        void  *user_data;
 
54
};
 
55
 
 
56
extern struct avro_allocator_state  AVRO_CURRENT_ALLOCATOR;
 
57
 
 
58
#define avro_realloc(ptr, osz, nsz)          \
 
59
        (AVRO_CURRENT_ALLOCATOR.alloc        \
 
60
         (AVRO_CURRENT_ALLOCATOR.user_data,  \
 
61
          (ptr), (osz), (nsz)))
 
62
 
 
63
#define avro_malloc(sz) (avro_realloc(NULL, 0, (sz)))
 
64
#define avro_free(ptr, osz) (avro_realloc((ptr), (osz), 0))
 
65
 
 
66
#define avro_new(type) (avro_realloc(NULL, 0, sizeof(type)))
 
67
#define avro_freet(type, ptr) (avro_realloc((ptr), sizeof(type), 0))
 
68
 
 
69
void *avro_calloc(size_t count, size_t size);
 
70
 
 
71
/*
 
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.
 
77
 *
 
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.
 
81
 */
 
82
 
 
83
char *avro_str_alloc(size_t str_size);
 
84
char *avro_strdup(const char *str);
 
85
void avro_str_free(char *str);
 
86
 
 
87
CLOSE_EXTERN
 
88
#endif