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

BLI_buffer.h « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e79d44fd934a8ed9eda4d6208ad56b424bbb3dfe (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
/*
 * 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.
 */

#pragma once

/** \file
 * \ingroup bli
 */

#ifdef __cplusplus
extern "C" {
#endif

typedef struct BLI_Buffer {
  void *data;
  const size_t elem_size;
  size_t count, alloc_count;
  int flag;
} BLI_Buffer;

enum {
  BLI_BUFFER_NOP = 0,
  BLI_BUFFER_USE_STATIC = (1 << 0),
};

#define BLI_buffer_declare_static(type_, name_, flag_, static_count_) \
  char name_##user; /* warn for free only */ \
  type_ name_##_static_[static_count_]; \
  BLI_Buffer name_ = { \
      (name_##_static_), sizeof(type_), 0, static_count_, BLI_BUFFER_USE_STATIC | (flag_)}

/* Never use static. */
#define BLI_buffer_declare(type_, name_, flag_) \
  bool name_##user; /* warn for free only */ \
  BLI_Buffer name_ = {NULL, sizeof(type_), 0, 0, (flag_)}

#define BLI_buffer_at(buffer_, type_, index_) \
  ((((type_ *)(buffer_) \
         ->data)[(BLI_assert(sizeof(type_) == (buffer_)->elem_size)), \
                 (BLI_assert((int)(index_) >= 0 && (size_t)(index_) < (buffer_)->count)), \
                 index_]))

#define BLI_buffer_array(buffer_, type_) (&(BLI_buffer_at(buffer_, type_, 0)))

#define BLI_buffer_resize_data(buffer_, type_, new_count_) \
  ((BLI_buffer_resize(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))

#define BLI_buffer_reinit_data(buffer_, type_, new_count_) \
  ((BLI_buffer_reinit(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))

#define BLI_buffer_append(buffer_, type_, val_) \
  (BLI_buffer_resize(buffer_, (buffer_)->count + 1), \
   (BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_))

#define BLI_buffer_clear(buffer_) \
  { \
    (buffer_)->count = 0; \
  } \
  (void)0

/**
 * \note Never decreases the amount of memory allocated.
 */
void BLI_buffer_resize(BLI_Buffer *buffer, size_t new_count);

/**
 * Ensure size, throwing away old data, respecting #BLI_BUFFER_USE_CALLOC.
 *
 * 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, size_t new_count);

/**
 * Append an array of elements.
 *
 * Callers use #BLI_buffer_append_array.
 */
void _bli_buffer_append_array(BLI_Buffer *buffer, void *data, size_t count);
#define BLI_buffer_append_array(buffer_, type_, data_, count_) \
  { \
    type_ *__tmp = (data_); \
    BLI_assert(sizeof(type_) == (buffer_)->elem_size); \
    _bli_buffer_append_array(buffer_, __tmp, count_); \
  } \
  (void)0

/**
 * Does not free the buffer structure itself.
 *
 * Callers use #BLI_buffer_free.
 */
void _bli_buffer_free(BLI_Buffer *buffer);
#define BLI_buffer_free(name_) \
  { \
    _bli_buffer_free(name_); \
    (void)name_##user; /* ensure we free */ \
  } \
  (void)0

/**
 * A buffer embedded in a struct. Using #memcpy is allowed until first resize.
 */
#define BLI_buffer_field_init(name_, type_) \
  { \
    memset(name_, 0, sizeof(*name_)); \
    *(size_t *)&((name_)->elem_size) = sizeof(type_); \
  } \
  (void)0

#define BLI_buffer_field_free(name_) _bli_buffer_free(name_)

#ifdef __cplusplus
}
#endif