Skip to content

Commit 669870c

Browse files
committed
Resync
1 parent bea5621 commit 669870c

4 files changed

Lines changed: 217 additions & 0 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1+
glsm/
12
*.o
3+
*.dll
4+
*.so
5+
*.dylib
6+
*.exe

include/array/rbuf.h

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* Copyright (C) 2010-2020 The RetroArch team
2+
*
3+
* ---------------------------------------------------------------------------------------
4+
* The following license statement only applies to this file (rbuf.h).
5+
* ---------------------------------------------------------------------------------------
6+
*
7+
* Permission is hereby granted, free of charge,
8+
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
9+
* to deal in the Software without restriction, including without limitation the rights to
10+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11+
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#ifndef __LIBRETRO_SDK_ARRAY_RBUF_H__
24+
#define __LIBRETRO_SDK_ARRAY_RBUF_H__
25+
26+
/*
27+
* This file implements stretchy buffers as invented (?) by Sean Barrett.
28+
* Based on the implementation from the public domain Bitwise project
29+
* by Per Vognsen - https://github.com/pervognsen/bitwise
30+
*
31+
* It's a super simple type safe dynamic array for C with no need
32+
* to predeclare any type or anything.
33+
* The first time an element is added, memory for 16 elements are allocated.
34+
* Then every time length is about to exceed capacity, capacity is doubled.
35+
*
36+
* Sample usage:
37+
*
38+
* mytype_t* buf = NULL;
39+
* RBUF_PUSH(buf, some_element);
40+
* RBUF_PUSH(buf, other_element);
41+
* -- now RBUF_LEN(buf) == 2, buf[0] == some_element, buf[1] == other_element
42+
*
43+
* -- Free allocated memory:
44+
* RBUF_FREE(buf);
45+
* -- now buf == NULL, RBUF_LEN(buf) == 0, RBUF_CAP(buf) == 0
46+
*
47+
* -- Explicitly increase allocated memory and set capacity:
48+
* RBUF_FIT(buf, 100);
49+
* -- now RBUF_LEN(buf) == 0, RBUF_CAP(buf) == 100
50+
*
51+
* -- Resize buffer (does not initialize or zero memory!)
52+
* RBUF_RESIZE(buf, 200);
53+
* -- now RBUF_LEN(buf) == 200, RBUF_CAP(buf) == 200
54+
*
55+
* -- To handle running out of memory:
56+
* bool ran_out_of_memory = !RBUF_TRYFIT(buf, 1000);
57+
* -- before RESIZE or PUSH. When out of memory, buf will stay unmodified.
58+
*/
59+
60+
#include <retro_math.h> /* for MAX */
61+
#include <stdlib.h> /* for malloc, realloc */
62+
63+
#define RBUF__HDR(b) (((struct rbuf__hdr *)(b))-1)
64+
65+
#define RBUF_LEN(b) ((b) ? RBUF__HDR(b)->len : 0)
66+
#define RBUF_CAP(b) ((b) ? RBUF__HDR(b)->cap : 0)
67+
#define RBUF_END(b) ((b) + RBUF_LEN(b))
68+
#define RBUF_SIZEOF(b) ((b) ? RBUF_LEN(b)*sizeof(*b) : 0)
69+
70+
#define RBUF_FREE(b) ((b) ? (free(RBUF__HDR(b)), (b) = NULL) : 0)
71+
#define RBUF_FIT(b, n) ((size_t)(n) <= RBUF_CAP(b) ? 0 : (*(void**)(&(b)) = rbuf__grow((b), (n), sizeof(*(b)))))
72+
#define RBUF_PUSH(b, val) (RBUF_FIT((b), 1 + RBUF_LEN(b)), (b)[RBUF__HDR(b)->len++] = (val))
73+
#define RBUF_POP(b) (b)[--RBUF__HDR(b)->len]
74+
#define RBUF_RESIZE(b, sz) (RBUF_FIT((b), (sz)), ((b) ? RBUF__HDR(b)->len = (sz) : 0))
75+
#define RBUF_CLEAR(b) ((b) ? RBUF__HDR(b)->len = 0 : 0)
76+
#define RBUF_TRYFIT(b, n) (RBUF_FIT((b), (n)), (((b) && RBUF_CAP(b) >= (size_t)(n)) || !(n)))
77+
78+
struct rbuf__hdr
79+
{
80+
size_t len;
81+
size_t cap;
82+
};
83+
84+
static void *rbuf__grow(void *buf,
85+
size_t new_len, size_t elem_size)
86+
{
87+
struct rbuf__hdr *new_hdr;
88+
size_t new_cap = MAX(2 * RBUF_CAP(buf), MAX(new_len, 16));
89+
size_t new_size = sizeof(struct rbuf__hdr) + new_cap*elem_size;
90+
if (buf)
91+
{
92+
new_hdr = (struct rbuf__hdr *)realloc(RBUF__HDR(buf), new_size);
93+
if (!new_hdr)
94+
return buf; /* out of memory, return unchanged */
95+
}
96+
else
97+
{
98+
new_hdr = (struct rbuf__hdr *)malloc(new_size);
99+
if (!new_hdr)
100+
return NULL; /* out of memory */
101+
new_hdr->len = 0;
102+
}
103+
new_hdr->cap = new_cap;
104+
return new_hdr + 1;
105+
}
106+
107+
#endif
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright (C) 2010-2020 The RetroArch team
2+
*
3+
* ---------------------------------------------------------------------------------------
4+
* The following license statement only applies to this file (snprintf_test.c).
5+
* ---------------------------------------------------------------------------------------
6+
*
7+
* Permission is hereby granted, free of charge,
8+
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
9+
* to deal in the Software without restriction, including without limitation the rights to
10+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11+
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#include <stdio.h>
24+
#include <stdlib.h>
25+
#include <stdint.h>
26+
#include <string.h>
27+
28+
#include <compat/strl.h>
29+
30+
int main(int argc, char *argv[])
31+
{
32+
char s[128];
33+
char *variable = "test1";
34+
char *variable2 = "test2";
35+
char *variable3 = "test3";
36+
char *variable4 = "test4";
37+
char *variable5 = "test5";
38+
char *variable6 = "test6";
39+
int ret = snprintf(s,
40+
sizeof(s), "%s%s%s%s%s%s%s%s%s%s%s", variable,
41+
" : ", variable2,
42+
" : ", variable3,
43+
" : ", variable4,
44+
" : ", variable5,
45+
" : ", variable6
46+
);
47+
48+
fprintf(stderr, "[%d], %s\n", ret, s);
49+
50+
return 0;
51+
}

samples/compat/strl/strl_test.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Copyright (C) 2010-2020 The RetroArch team
2+
*
3+
* ---------------------------------------------------------------------------------------
4+
* The following license statement only applies to this file (strl_test.c).
5+
* ---------------------------------------------------------------------------------------
6+
*
7+
* Permission is hereby granted, free of charge,
8+
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
9+
* to deal in the Software without restriction, including without limitation the rights to
10+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11+
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#include <stdio.h>
24+
#include <stdlib.h>
25+
#include <stdint.h>
26+
#include <string.h>
27+
28+
#include <compat/strl.h>
29+
30+
int main(int argc, char *argv[])
31+
{
32+
char s[128];
33+
char *variable = "test1";
34+
char *variable2 = "test2";
35+
char *variable3 = "test3";
36+
char *variable4 = "test4";
37+
char *variable5 = "test5";
38+
char *variable6 = "test6";
39+
int ret = strlcpy(s, variable, sizeof(s));
40+
ret = strlcat(s, " : ", sizeof(s));
41+
ret = strlcat(s, variable2,sizeof(s));
42+
ret = strlcat(s, " : ", sizeof(s));
43+
ret = strlcat(s, variable3,sizeof(s));
44+
ret = strlcat(s, " : ", sizeof(s));
45+
ret = strlcat(s, variable4,sizeof(s));
46+
ret = strlcat(s, " : ", sizeof(s));
47+
ret = strlcat(s, variable5,sizeof(s));
48+
ret = strlcat(s, " : ", sizeof(s));
49+
ret = strlcat(s, variable6,sizeof(s));
50+
51+
fprintf(stderr, "[%d], %s\n", ret, s);
52+
53+
return 0;
54+
}

0 commit comments

Comments
 (0)