Welcome to mirror list, hosted at ThFree Co, Russian Federation.

buffer.c « intern « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 74e97d8943057039fcd21653ac0baf6ca3b191a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/** \file
 * \ingroup bli
 *
 * Primitive generic buffer library.
 *
 * - Automatically grow as needed.
 *   (currently never shrinks).
 * - Can be passed between functions.
 * - Supports using stack memory by default,
 *   falling back to heap as needed.
 *
 * Usage examples:
 * \code{.c}
 * BLI_buffer_declare_static(int, my_int_array, BLI_BUFFER_NOP, 32);
 *
 * BLI_buffer_append(my_int_array, int, 42);
 * BLI_assert(my_int_array.count == 1);
 * BLI_assert(BLI_buffer_at(my_int_array, int, 0) == 42);
 *
 * BLI_buffer_free(&my_int_array);
 * \endcode
 *
 * \note this more or less fills same purpose as #BLI_array,
 * but supports resizing the array outside of the function
 * it was declared in.
 */

#include <string.h>

#include "MEM_guardedalloc.h"

#include "BLI_buffer.h"
#include "BLI_utildefines.h"

#include "BLI_strict_flags.h"

static void *buffer_alloc(BLI_Buffer *buffer, const size_t len)
{
  return MEM_mallocN(buffer->elem_size * len, "BLI_Buffer.data");
}

static void *buffer_realloc(BLI_Buffer *buffer, const size_t len)
{
  return MEM_reallocN_id(buffer->data, buffer->elem_size * len, "BLI_Buffer.data");
}

void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count)
{
  if (UNLIKELY(new_count > buffer->alloc_count)) {
    if (buffer->flag & BLI_BUFFER_USE_STATIC) {
      void *orig = buffer->data;

      buffer->data = buffer_alloc(buffer, new_count);
      memcpy(buffer->data, orig, buffer->elem_size * buffer->count);
      buffer->alloc_count = new_count;
      buffer->flag &= ~BLI_BUFFER_USE_STATIC;
    }
    else {
      if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
        buffer->alloc_count *= 2;
      }
      else {
        buffer->alloc_count = new_count;
      }

      buffer->data = buffer_realloc(buffer, buffer->alloc_count);
    }
  }

  buffer->count = new_count;
}

/**
 * Similar to #BLI_buffer_resize, but use when the existing data can be:
 * - Ignored (malloc'd)
 * - Cleared (when BLI_BUFFER_USE_CALLOC is set)
 */
void BLI_buffer_reinit(BLI_Buffer *buffer, const size_t new_count)
{
  if (UNLIKELY(new_count > buffer->alloc_count)) {
    if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
      if (buffer->data) {
        MEM_freeN(buffer->data);
      }
    }

    if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
      buffer->alloc_count *= 2;
    }
    else {
      buffer->alloc_count = new_count;
    }

    buffer->flag &= ~BLI_BUFFER_USE_STATIC;
    buffer->data = buffer_alloc(buffer, buffer->alloc_count);
  }

  buffer->count = new_count;
}

/* Callers use BLI_buffer_append_array. */
void _bli_buffer_append_array(BLI_Buffer *buffer, void *new_data, size_t count)
{
  size_t size = buffer->count;
  BLI_buffer_resize(buffer, size + count);

  uint8_t *bytes = (uint8_t *)buffer->data;
  memcpy(bytes + size * buffer->elem_size, new_data, count * buffer->elem_size);
}

/* callers use BLI_buffer_free */
void _bli_buffer_free(BLI_Buffer *buffer)
{
  if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
    if (buffer->data) {
      MEM_freeN(buffer->data);
    }
  }
  memset(buffer, 0, sizeof(*buffer));
}