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

« back to all changes in this revision

Viewing changes to src/array.c

  • 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
#include <errno.h>
 
19
#include <stdlib.h>
 
20
#include <string.h>
 
21
 
 
22
#include "avro/allocation.h"
 
23
#include "avro/data.h"
 
24
#include "avro/errors.h"
 
25
#include "avro_private.h"
 
26
 
 
27
 
 
28
void avro_raw_array_init(avro_raw_array_t *array, size_t element_size)
 
29
{
 
30
        memset(array, 0, sizeof(avro_raw_array_t));
 
31
        array->element_size = element_size;
 
32
}
 
33
 
 
34
 
 
35
void avro_raw_array_done(avro_raw_array_t *array)
 
36
{
 
37
        if (array->data) {
 
38
                avro_free(array->data, array->allocated_size);
 
39
        }
 
40
        memset(array, 0, sizeof(avro_raw_array_t));
 
41
}
 
42
 
 
43
 
 
44
void avro_raw_array_clear(avro_raw_array_t *array)
 
45
{
 
46
        array->element_count = 0;
 
47
}
 
48
 
 
49
 
 
50
int
 
51
avro_raw_array_ensure_size(avro_raw_array_t *array, size_t desired_count)
 
52
{
 
53
        size_t  required_size = array->element_size * desired_count;
 
54
        if (array->allocated_size >= required_size) {
 
55
                return 0;
 
56
        }
 
57
 
 
58
        /*
 
59
         * Double the old size when reallocating.
 
60
         */
 
61
 
 
62
        size_t  new_size;
 
63
        if (array->allocated_size == 0) {
 
64
                /*
 
65
                 * Start with an arbitrary 10 items.
 
66
                 */
 
67
 
 
68
                new_size = 10 * array->element_size;
 
69
        } else {
 
70
                new_size = array->allocated_size * 2;
 
71
        }
 
72
 
 
73
        if (required_size > new_size) {
 
74
                new_size = required_size;
 
75
        }
 
76
 
 
77
        array->data = avro_realloc(array->data, array->allocated_size, new_size);
 
78
        if (array->data == NULL) {
 
79
                avro_set_error("Cannot allocate space in array for %" PRIsz " elements",
 
80
                               desired_count);
 
81
                return ENOMEM;
 
82
        }
 
83
        array->allocated_size = new_size;
 
84
 
 
85
        return 0;
 
86
}
 
87
 
 
88
 
 
89
int
 
90
avro_raw_array_ensure_size0(avro_raw_array_t *array, size_t desired_count)
 
91
{
 
92
        int  rval;
 
93
        size_t  old_allocated_size = array->allocated_size;
 
94
        check(rval, avro_raw_array_ensure_size(array, desired_count));
 
95
 
 
96
        if (array->allocated_size > old_allocated_size) {
 
97
                size_t  extra_space = array->allocated_size - old_allocated_size;
 
98
                void  *buf = array->data;
 
99
                memset((char *)buf + old_allocated_size, 0, extra_space);
 
100
        }
 
101
 
 
102
        return 0;
 
103
}
 
104
 
 
105
 
 
106
void *avro_raw_array_append(avro_raw_array_t *array)
 
107
{
 
108
        int  rval;
 
109
 
 
110
        rval = avro_raw_array_ensure_size(array, array->element_count + 1);
 
111
        if (rval) {
 
112
                return NULL;
 
113
        }
 
114
 
 
115
        size_t  offset = array->element_size * array->element_count;
 
116
        array->element_count++;
 
117
        return (char *)array->data + offset;
 
118
}