Skip to content

Commit 2afe77a

Browse files
update use of mutexes
1 parent 6df1faa commit 2afe77a

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

src/arena.c

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,39 @@ void cmark_arena_push(void) {
3636
}
3737

3838
int cmark_arena_pop(void) {
39+
int ret = 1;
40+
CMARK_INITIALIZE_AND_LOCK(arena);
3941
if (!A)
40-
return 0;
41-
while (A && !A->push_point) {
42-
free(A->ptr);
43-
struct arena_chunk *n = A->prev;
44-
free(A);
45-
A = n;
42+
ret = 0;
43+
else {
44+
while (A && !A->push_point) {
45+
free(A->ptr);
46+
struct arena_chunk *n = A->prev;
47+
free(A);
48+
A = n;
49+
}
50+
if (A)
51+
A->push_point = 0;
4652
}
47-
if (A)
48-
A->push_point = 0;
49-
return 1;
53+
CMARK_UNLOCK(arena);
54+
return ret;
5055
}
5156

5257
static void init_arena(void) {
58+
CMARK_INITIALIZE_AND_LOCK(arena);
5359
A = alloc_arena_chunk(4 * 1048576, NULL);
60+
CMARK_UNLOCK(arena);
5461
}
5562

5663
void cmark_arena_reset(void) {
64+
CMARK_INITIALIZE_AND_LOCK(arena);
5765
while (A) {
5866
free(A->ptr);
5967
struct arena_chunk *n = A->prev;
6068
free(A);
6169
A = n;
6270
}
71+
CMARK_UNLOCK(arena);
6372
}
6473

6574
static void *arena_calloc(size_t nmem, size_t size) {
@@ -74,20 +83,23 @@ static void *arena_calloc(size_t nmem, size_t size) {
7483
sz = (sz + align) & ~align;
7584

7685
CMARK_INITIALIZE_AND_LOCK(arena);
77-
86+
87+
void *ptr = NULL;
88+
7889
if (sz > A->sz) {
7990
A->prev = alloc_arena_chunk(sz, A->prev);
80-
return (uint8_t *) A->prev->ptr + sizeof(size_t);
81-
}
82-
if (sz > A->sz - A->used) {
83-
A = alloc_arena_chunk(A->sz + A->sz / 2, A);
91+
ptr = (uint8_t *) A->prev->ptr;
92+
} else {
93+
if (sz > A->sz - A->used) {
94+
A = alloc_arena_chunk(A->sz + A->sz / 2, A);
95+
}
96+
ptr = (uint8_t *) A->ptr + A->used;
97+
A->used += sz;
98+
*((size_t *) ptr) = sz - sizeof(size_t);
8499
}
85-
void *ptr = (uint8_t *) A->ptr + A->used;
86-
A->used += sz;
87100

88101
CMARK_UNLOCK(arena);
89-
90-
*((size_t *) ptr) = sz - sizeof(size_t);
102+
91103
return (uint8_t *) ptr + sizeof(size_t);
92104
}
93105

src/inlines.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,16 +1376,23 @@ static const char SMART_PUNCT_CHARS[] = {
13761376

13771377
static bufsize_t subject_find_special_char(subject *subj, int options) {
13781378
bufsize_t n = subj->pos + 1;
1379+
bufsize_t ret = subj->input.len;
13791380

1381+
CMARK_INITIALIZE_AND_LOCK(chars);
13801382
while (n < subj->input.len) {
1381-
if (SPECIAL_CHARS[subj->input.data[n]])
1382-
return n;
1383-
if (options & CMARK_OPT_SMART && SMART_PUNCT_CHARS[subj->input.data[n]])
1384-
return n;
1383+
if (SPECIAL_CHARS[subj->input.data[n]]) {
1384+
ret = n;
1385+
break;
1386+
}
1387+
if (options & CMARK_OPT_SMART && SMART_PUNCT_CHARS[subj->input.data[n]]) {
1388+
ret = n;
1389+
break;
1390+
}
13851391
n++;
13861392
}
1393+
CMARK_UNLOCK(chars);
13871394

1388-
return subj->input.len;
1395+
return ret;
13891396
}
13901397

13911398
void cmark_inlines_add_special_character(unsigned char c, bool emphasis) {

0 commit comments

Comments
 (0)