1 mike 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 mike 1.1 **==============================================================================
23 */
24
25 #include <string.h>
26 #include <stdlib.h>
27 #include "buffer.h"
28 #include "heap.h"
29 #include "state.h"
30
31 #if 0 /* ATTN:REMOVE */
32 int Buffer_Init(Buffer* self, size_t capacity)
33 {
34 size_t r = 16;
35
36 while (r < capacity)
37 r *= 2;
38
39 if (!(self->data = (char*)MOF_Malloc(&state.heap, r)))
40 return -1;
41
42 self->size = 0;
43 mike 1.1 self->capacity = r;
44
45 return 0;
46 }
47 #endif
48
49 int Buffer_Append(Buffer* self, const void* data , size_t size)
50 {
51 size_t capacity;
52
53 if (!self || !data)
54 return -1;
55
56 capacity = self->size + size;
57
58 /* Grow buffer if neceessary */
59 if (capacity > self->capacity)
60 {
61 size_t r = 16;
62
63 while (r < capacity)
64 mike 1.1 r *= 2;
65
66 self->data = (char*)MOF_Realloc(&state.heap, self->data, r);
67
68 if (!self->data)
69 return -1;
70
71 self->capacity = r;
72 }
73
74 /* Copy the data */
75 memcpy((char*)self->data + self->size, data, size);
76 self->size += size;
77
78 return 0;
79 }
|