-
Notifications
You must be signed in to change notification settings - Fork 24
/
buffer.c
79 lines (67 loc) · 2.36 KB
/
buffer.c
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
/* PANDAseq -- Assemble paired FASTQ Illumina reads and strip the region between amplification primers.
Copyright (C) 2011-2012 Andre Masella
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_PTHREAD
# include <pthread.h>
#endif
#include "buffer.h"
PandaDebug panda_debug_flags = PANDA_DEBUG_DEFAULT;
#if HAVE_PTHREAD
# define BUFFER(name, type, size) pthread_key_t PANDACONCAT(name, _key);
# include "buffer.list"
# undef BUFFER
__attribute__ ((constructor))
static void lib_init(
void) {
# define BUFFER(name, type, size) pthread_key_create(&PANDACONCAT(name, _key), free);
# include "buffer.list"
# undef BUFFER
}
__attribute__ ((destructor))
void lib_destroy(
void) {
# define BUFFER(name, type, size) free(pthread_getspecific(PANDACONCAT(name, _key))); pthread_key_delete(PANDACONCAT(name, _key));
# include "buffer.list"
# undef BUFFER
}
static void *get_buffer(
pthread_key_t key,
size_t size) {
void *buffer;
if ((buffer = pthread_getspecific(key)) == NULL) {
buffer = malloc(size);
pthread_setspecific(key, buffer);
}
return buffer;
}
# define BUFFER(name, type, size) type *PANDACONCAT(name, _buffer)(void) { return get_buffer(PANDACONCAT(name, _key), sizeof(type) * size); }
# include "buffer.list"
# undef BUFFER
#else
# define BUFFER(name, type, size) static type PANDACONCAT(name, buffer)[size]; type *PANDACONCAT(name, _buffer)(void) { return PANDACONCAT(name, buffer); }
# include "buffer.list"
# undef BUFFER
#endif
void bufferprintf(
char *buffer,
char *fmt,
...) {
va_list va;
va_start(va, fmt);
(void) vsnprintf(buffer, BUFFER_SIZE, fmt, va);
va_end(va);
}