Dynamic Load Balance 3.6.1+32-59d1
small_array.h
Go to the documentation of this file.
1/*********************************************************************************/
2/* Copyright 2009-2024 Barcelona Supercomputing Center */
3/* */
4/* This file is part of the DLB library. */
5/* */
6/* DLB is free software: you can redistribute it and/or modify */
7/* it under the terms of the GNU Lesser General Public License as published by */
8/* the Free Software Foundation, either version 3 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* DLB is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU Lesser General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU Lesser General Public License */
17/* along with DLB. If not, see <https://www.gnu.org/licenses/>. */
18/*********************************************************************************/
19
20/* Credits: https://nullprogram.com/blog/2016/10/07/ */
21
22#ifndef SMALL_ARRAY_H
23#define SMALL_ARRAY_H
24
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28
29#include "support/queues.h"
30
31#include <sys/types.h>
32#include <stdlib.h>
33
34#if NCPUS_AT_CONFIGURE_TIME > 1 && NCPUS_AT_CONFIGURE_TIME < 1025
35enum { SMALL_ARRAY_DEFAULT_SIZE = NCPUS_AT_CONFIGURE_TIME };
36#else
38#endif
39
40
41/*** SmallArray for int types ***/
42
43typedef struct SmallArrayInt {
44 size_t size;
45 int *values;
49
50static inline int* small_array_int_init(small_array_int_t *array, size_t size) {
51 array->size = size;
52 if (size <= SMALL_ARRAY_DEFAULT_SIZE) {
53 array->values = array->buffer;
54 } else {
55 array->values = malloc(sizeof(int) * size);
56 }
57 return array->values;
58}
59
60static inline void small_array_int_free(small_array_int_t *array) {
61 if (array->size > SMALL_ARRAY_DEFAULT_SIZE) {
62 free(array->values);
63 }
64 array->values = NULL;
65}
66
67
68/*** SmallArray for pid_t types ***/
69
70typedef struct SmallArrayPID {
71 size_t size;
72 pid_t *values;
75
76static inline pid_t* small_array_pid_t_init(small_array_pid_t *array, size_t size) {
77 array->size = size;
78 if (size <= SMALL_ARRAY_DEFAULT_SIZE) {
79 array->values = array->buffer;
80 } else {
81 array->values = malloc(sizeof(pid_t) * size);
82 }
83 return array->values;
84}
85
86static inline void small_array_pid_t_free(small_array_pid_t *array) {
87 if (array->size > SMALL_ARRAY_DEFAULT_SIZE) {
88 free(array->values);
89 }
90 array->values = NULL;
91}
92
93
94/*** SmallArray for cpuid_t types ***/
95
96typedef struct SmallArrayCpuid {
97 size_t size;
101
102static inline cpuid_t* small_array_cpuid_t_init(small_array_cpuid_t *array, size_t size) {
103 array->size = size;
104 if (size <= SMALL_ARRAY_DEFAULT_SIZE) {
105 array->values = array->buffer;
106 } else {
107 array->values = malloc(sizeof(cpuid_t) * size);
108 }
109 return array->values;
110}
111
112static inline void small_array_cpuid_t_free(small_array_cpuid_t *array) {
113 if (array->size > SMALL_ARRAY_DEFAULT_SIZE) {
114 free(array->values);
115 }
116 array->values = NULL;
117}
118
119
120/*** SmallArray for lewi_request_t types ***/
121
122typedef struct SmallArrayLeWIRequest {
123 size_t size;
127
128static inline lewi_request_t* small_array_lewi_request_t_init(
129 small_array_lewi_request_t *array, size_t size) {
130 array->size = size;
131 if (size <= SMALL_ARRAY_DEFAULT_SIZE) {
132 array->values = array->buffer;
133 } else {
134 array->values = malloc(sizeof(lewi_request_t) * size);
135 }
136 return array->values;
137}
138
139static inline void small_array_lewi_request_t_free(small_array_lewi_request_t *array) {
140 if (array->size > SMALL_ARRAY_DEFAULT_SIZE) {
141 free(array->values);
142 }
143 array->values = NULL;
144}
145
146#define SMALL_ARRAY(type, var_name, size) \
147 small_array_##type __##var_name __attribute__ ((__cleanup__(small_array_##type##_free))); \
148 type *var_name = small_array_##type##_init(&__##var_name, size);
149
150
151#endif /* SMALL_ARRAY_H */
@ SMALL_ARRAY_DEFAULT_SIZE
Definition: small_array.h:37
small_array_int_t small_array_int
Definition: small_array.h:48
Definition: queues.h:35
Definition: small_array.h:96
cpuid_t buffer[SMALL_ARRAY_DEFAULT_SIZE]
Definition: small_array.h:99
size_t size
Definition: small_array.h:97
cpuid_t * values
Definition: small_array.h:98
Definition: small_array.h:43
int * values
Definition: small_array.h:45
size_t size
Definition: small_array.h:44
int buffer[SMALL_ARRAY_DEFAULT_SIZE]
Definition: small_array.h:46
Definition: small_array.h:122
lewi_request_t * values
Definition: small_array.h:124
lewi_request_t buffer[SMALL_ARRAY_DEFAULT_SIZE]
Definition: small_array.h:125
size_t size
Definition: small_array.h:123
Definition: small_array.h:70
size_t size
Definition: small_array.h:71
pid_t buffer[SMALL_ARRAY_DEFAULT_SIZE]
Definition: small_array.h:73
pid_t * values
Definition: small_array.h:72
uint8_t cpuid_t
Definition: types.h:32