summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_slstr.c
blob: 017b692fe7e9af990aa2b6a1f213b1adab68c287 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#define EINA_SLSTR_INTERNAL

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "Eina.h"
#include "eina_private.h"

// ========================================================================= //

static int _slstr_init = 0;
static Eina_FreeQ *_slstr_main_fq = NULL;
static Eina_TLS _slstr_tls = 0;

// ========================================================================= //

#if 0
// 2 extension ideas here: slices for short-lived raw data buffers
EAPI Eina_Rw_Slice eina_slslice_new(size_t length); // alloc
EAPI Eina_Rw_Slice eina_slslice_copy(Eina_Slice slice); // copies
EAPI Eina_Rw_Slice eina_slslice_free(Eina_Rw_Slice slice); // steals
#endif

static void
_slstr_tls_free_cb(void *ptr)
{
   Eina_FreeQ *fq = ptr;

   eina_freeq_free(fq);
}

Eina_Bool
eina_slstr_init(void)
{
   if (_slstr_init++) return EINA_TRUE;

   _slstr_main_fq = eina_freeq_new(EINA_FREEQ_POSTPONED);
   if (!_slstr_main_fq) goto fail;
   if (!eina_tls_cb_new(&_slstr_tls, _slstr_tls_free_cb)) goto fail_tls;

   return EINA_TRUE;

fail_tls:
   eina_tls_free(_slstr_tls);
   _slstr_tls = 0;
fail:
   eina_freeq_free(_slstr_main_fq);
   _slstr_main_fq = NULL;
   return EINA_FALSE;
}

Eina_Bool
eina_slstr_shutdown(void)
{
   if (_slstr_init == 0) return EINA_FALSE;
   if (--_slstr_init) return EINA_TRUE;

   eina_freeq_free(_slstr_main_fq);
   eina_tls_free(_slstr_tls);
   _slstr_main_fq = NULL;
   _slstr_tls = 0;

   return EINA_TRUE;
}

static inline Eina_FreeQ *
_slstr_freeq_get(Eina_Bool nocreate)
{
   if (eina_main_loop_is())
     return _slstr_main_fq;
   else
     {
        Eina_FreeQ *fq;

        fq = eina_tls_get(_slstr_tls);
        if (!nocreate && EINA_UNLIKELY(!fq))
          {
             fq = eina_freeq_new(EINA_FREEQ_POSTPONED);
             eina_tls_set(_slstr_tls, fq);
          }
        return fq;
     }
}

EAPI Eina_Slstr *
eina_slstr_copy_new(const char *string)
{
   Eina_FreeQ *fq;
   char *copy;
   size_t len = 0;

   if (!string) return NULL;

   fq = _slstr_freeq_get(EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(fq, NULL);

   copy = eina_strdup(string);
   if (!copy) return NULL;
#ifdef DEBUG
   len = strlen(string) + 1;
#endif
   eina_freeq_ptr_add(fq, copy, free, len);
   return copy;
}

EAPI Eina_Slstr *
eina_slstr_copy_new_length(const char *string, size_t len)
{
   Eina_FreeQ *fq;
   char *copy;

   if (!string) return NULL;

   fq = _slstr_freeq_get(EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(fq, NULL);

   copy = eina_strndup(string, len);
   if (!copy) return NULL;
   eina_freeq_ptr_add(fq, copy, free, len);
   return copy;
}

EAPI Eina_Slstr *
eina_slstr_steal_new(char *string)
{
   Eina_FreeQ *fq;
   size_t len = 0;

   if (!string) return NULL;

   fq = _slstr_freeq_get(EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(fq, NULL);

#ifdef DEBUG
   len = strlen(string) + 1;
#endif
   eina_freeq_ptr_add(fq, string, free, len);
   return string;
}

EAPI Eina_Slstr *
eina_slstr_stringshare_new(Eina_Stringshare *string)
{
   Eina_FreeQ *fq;
   size_t len = 0;

   if (!string) return NULL;

   fq = _slstr_freeq_get(EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(fq, NULL);

#ifdef DEBUG
   len = eina_stringshare_strlen(string) + 1;
#endif
   eina_freeq_ptr_add(fq, (void *) string, EINA_FREE_CB(eina_stringshare_del), len);
   return string;
}

EAPI Eina_Slstr *
eina_slstr_tmpstr_new(Eina_Tmpstr *string)
{
   Eina_FreeQ *fq;
   size_t len = 0;

   if (!string) return NULL;

   fq = _slstr_freeq_get(EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(fq, NULL);

#ifdef DEBUG
   len = eina_tmpstr_strlen(string) + 1;
#endif
   eina_freeq_ptr_add(fq, (void *) string, EINA_FREE_CB(eina_tmpstr_del), len);
   return string;
}

EAPI Eina_Slstr *
eina_slstr_strbuf_new(Eina_Strbuf *string)
{
   Eina_FreeQ *fq;
   size_t len = 0;
   char *str;

   if (!string) return NULL;

   fq = _slstr_freeq_get(EINA_FALSE);
   EINA_SAFETY_ON_NULL_GOTO(fq, error);

   str = eina_strbuf_release(string);
#ifdef DEBUG
   len = eina_strbuf_length_get(string) + 1;
#endif
   eina_freeq_ptr_add(fq, str, free, len);
   return str;

error:
   eina_strbuf_free(string);
   return NULL;
}

EAPI Eina_Slstr *
eina_slstr_vasprintf_new(const char *fmt, va_list args)
{
   Eina_FreeQ *fq;
   size_t len = 0;
   char *str;
   int r;

   fq = _slstr_freeq_get(EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(fq, NULL);

   r = vasprintf(&str, fmt, args);
   if (r == -1) return NULL;

#ifdef DEBUG
   len = r + 1;
#endif
   eina_freeq_ptr_add(fq, str, free, len);
   return str;
}

EAPI void
eina_slstr_local_clear(void)
{
   Eina_FreeQ *fq;

   fq = _slstr_freeq_get(EINA_TRUE);
   if (!fq) return;

   eina_freeq_clear(fq);
}