Skip to content

Commit 99fdb5f

Browse files
committed
Try to solve issues with python and iterators.
1 parent 27c005d commit 99fdb5f

File tree

33 files changed

+423
-513
lines changed

33 files changed

+423
-513
lines changed

cmake/CompileOptions.cmake

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,55 @@ if (PROJECT_OS_FAMILY MATCHES "unix" OR PROJECT_OS_FAMILY MATCHES "macos")
427427
endif()
428428
endif()
429429

430+
macro(check_symbol_executable symbol binary_path result_var)
431+
if(WIN32)
432+
find_program(DUMPBIN_EXECUTABLE dumpbin)
433+
if(NOT DUMPBIN_EXECUTABLE)
434+
message(FATAL_ERROR "Trying to find symbol ${symbol} in ${binary_path} but dumpbin was not found")
435+
endif()
436+
execute_process(
437+
COMMAND ${DUMPBIN_EXECUTABLE} /symbols ${binary_path}
438+
OUTPUT_VARIABLE dumpbin_output
439+
RESULT_VARIABLE dumpbin_result
440+
ERROR_QUIET
441+
OUTPUT_STRIP_TRAILING_WHITESPACE
442+
)
443+
string(FIND "${dumpbin_output}" symbol SYMBOL_FOUND)
444+
if(NOT SYMBOL_FOUND EQUAL -1)
445+
set(${result_var} TRUE PARENT_SCOPE)
446+
else()
447+
set(${result_var} FALSE PARENT_SCOPE)
448+
endif()
449+
else()
450+
find_program(NM_EXECUTABLE nm)
451+
if(NM_EXECUTABLE)
452+
execute_process(
453+
COMMAND ${NM_EXECUTABLE} -D ${binary_path}
454+
OUTPUT_VARIABLE nm_output
455+
RESULT_VARIABLE nm_result
456+
ERROR_QUIET
457+
OUTPUT_STRIP_TRAILING_WHITESPACE
458+
)
459+
string(FIND "${nm_output}" symbol SYMBOL_FOUND)
460+
if(NOT SYMBOL_FOUND EQUAL -1)
461+
set(${result_var} TRUE PARENT_SCOPE)
462+
else()
463+
set(${result_var} FALSE PARENT_SCOPE)
464+
endif()
465+
else()
466+
message(FATAL_ERROR "Trying to find symbol ${symbol} in ${binary_path} but nm was not found")
467+
endif()
468+
endif()
469+
endmacro()
470+
471+
function(check_asan_executable binary_path result_var)
472+
check_symbol_executable("__asan_init" "${binary_path}" ${result_var})
473+
endfunction()
474+
475+
function(check_tsan_executable binary_path result_var)
476+
check_symbol_executable("__tsan_init" "${binary_path}" ${result_var})
477+
endfunction()
478+
430479
#
431480
# Linker options
432481
#

source/adt/include/adt/adt_map.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ typedef int (*map_cb_iterate)(map, map_key, map_value, map_cb_iterate_args);
5151

5252
typedef struct map_iterator_type *map_iterator;
5353

54+
/* -- Member Data -- */
55+
56+
struct map_iterator_type
57+
{
58+
map m;
59+
size_t current_bucket;
60+
size_t current_pair;
61+
};
62+
5463
/* -- Methods -- */
5564

5665
ADT_API map map_create(map_cb_hash hash_cb, map_cb_compare compare_cb);
@@ -79,15 +88,15 @@ ADT_API int map_clear(map m);
7988

8089
ADT_API void map_destroy(map m);
8190

82-
ADT_API map_iterator map_iterator_begin(map m);
91+
ADT_API void map_iterator_begin(map_iterator it, map m);
8392

8493
ADT_API map_key map_iterator_key(map_iterator it);
8594

8695
ADT_API map_value map_iterator_value(map_iterator it);
8796

8897
ADT_API void map_iterator_next(map_iterator it);
8998

90-
ADT_API int map_iterator_end(map_iterator *it);
99+
ADT_API int map_iterator_end(map_iterator it);
91100

92101
#ifdef __cplusplus
93102
}

source/adt/include/adt/adt_set.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ typedef int (*set_cb_iterate)(set, set_key, set_value, set_cb_iterate_args);
5050

5151
typedef struct set_iterator_type *set_iterator;
5252

53+
/* -- Member Data -- */
54+
55+
struct set_iterator_type
56+
{
57+
set s;
58+
size_t current_bucket;
59+
size_t current_pair;
60+
};
61+
5362
/* -- Methods -- */
5463

5564
ADT_API set set_create(set_cb_hash hash_cb, set_cb_compare compare_cb);
@@ -80,15 +89,15 @@ ADT_API int set_clear(set s);
8089

8190
ADT_API void set_destroy(set s);
8291

83-
ADT_API set_iterator set_iterator_begin(set s);
92+
ADT_API void set_iterator_begin(set_iterator it, set s);
8493

8594
ADT_API set_key set_iterator_key(set_iterator it);
8695

8796
ADT_API set_value set_iterator_value(set_iterator it);
8897

8998
ADT_API void set_iterator_next(set_iterator it);
9099

91-
ADT_API int set_iterator_end(set_iterator *it);
100+
ADT_API int set_iterator_end(set_iterator it);
92101

93102
#ifdef __cplusplus
94103
}

source/adt/source/adt_map.c

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ struct map_type
4242
map_cb_compare compare_cb;
4343
};
4444

45-
struct map_iterator_type
46-
{
47-
map m;
48-
size_t current_bucket;
49-
size_t current_pair;
50-
};
51-
5245
/* -- Methods -- */
5346

5447
map map_create(map_cb_hash hash_cb, map_cb_compare compare_cb)
@@ -506,30 +499,29 @@ void map_destroy(map m)
506499
free(m);
507500
}
508501

509-
map_iterator map_iterator_begin(map m)
502+
void map_iterator_begin(map_iterator it, map m)
510503
{
511-
if (m != NULL && m->buckets != NULL && map_size(m) > 0)
504+
if (it != NULL)
512505
{
513-
map_iterator it = malloc(sizeof(struct map_iterator_type));
506+
it->current_bucket = 0;
507+
it->current_pair = 0;
514508

515-
if (it != NULL)
509+
if (m != NULL && m->buckets != NULL && map_size(m) > 0)
516510
{
517511
it->m = m;
518-
it->current_bucket = 0;
519-
it->current_pair = 0;
520512

521513
map_iterator_next(it);
522-
523-
return it;
514+
}
515+
else
516+
{
517+
it->m = NULL;
524518
}
525519
}
526-
527-
return NULL;
528520
}
529521

530522
map_key map_iterator_key(map_iterator it)
531523
{
532-
if (it != NULL && it->current_bucket < it->m->capacity && it->current_pair > 0)
524+
if (it != NULL && it->m != NULL && it->current_bucket < it->m->capacity && it->current_pair > 0)
533525
{
534526
return it->m->buckets[it->current_bucket].pairs[it->current_pair - 1].key;
535527
}
@@ -539,7 +531,7 @@ map_key map_iterator_key(map_iterator it)
539531

540532
map_value map_iterator_value(map_iterator it)
541533
{
542-
if (it != NULL && it->current_bucket < it->m->capacity && it->current_pair > 0)
534+
if (it != NULL && it->m != NULL && it->current_bucket < it->m->capacity && it->current_pair > 0)
543535
{
544536
return it->m->buckets[it->current_bucket].pairs[it->current_pair - 1].value;
545537
}
@@ -549,7 +541,7 @@ map_value map_iterator_value(map_iterator it)
549541

550542
void map_iterator_next(map_iterator it)
551543
{
552-
if (it != NULL)
544+
if (it != NULL && it->m != NULL)
553545
{
554546
for (; it->current_bucket < it->m->capacity; ++it->current_bucket)
555547
{
@@ -575,16 +567,12 @@ void map_iterator_next(map_iterator it)
575567
}
576568
}
577569

578-
int map_iterator_end(map_iterator *it)
570+
int map_iterator_end(map_iterator it)
579571
{
580-
if (it != NULL && *it != NULL)
572+
if (it != NULL && it->m != NULL)
581573
{
582-
if ((*it)->current_bucket >= (*it)->m->capacity)
574+
if (it->current_bucket >= it->m->capacity)
583575
{
584-
free(*it);
585-
586-
*it = NULL;
587-
588576
return 0;
589577
}
590578

source/adt/source/adt_set.c

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ struct set_type
4242
set_cb_compare compare_cb;
4343
};
4444

45-
struct set_iterator_type
46-
{
47-
set s;
48-
size_t current_bucket;
49-
size_t current_pair;
50-
};
51-
5245
/* -- Methods -- */
5346

5447
set set_create(set_cb_hash hash_cb, set_cb_compare compare_cb)
@@ -521,30 +514,29 @@ void set_destroy(set s)
521514
free(s);
522515
}
523516

524-
set_iterator set_iterator_begin(set s)
517+
void set_iterator_begin(set_iterator it, set s)
525518
{
526-
if (s != NULL && s->buckets != NULL && set_size(s) > 0)
519+
if (it != NULL)
527520
{
528-
set_iterator it = malloc(sizeof(struct set_iterator_type));
521+
it->current_bucket = 0;
522+
it->current_pair = 0;
529523

530-
if (it != NULL)
524+
if (s != NULL && s->buckets != NULL && set_size(s) > 0)
531525
{
532526
it->s = s;
533-
it->current_bucket = 0;
534-
it->current_pair = 0;
535527

536528
set_iterator_next(it);
537-
538-
return it;
529+
}
530+
else
531+
{
532+
it->s = NULL;
539533
}
540534
}
541-
542-
return NULL;
543535
}
544536

545537
set_key set_iterator_key(set_iterator it)
546538
{
547-
if (it != NULL && it->current_bucket < it->s->capacity && it->current_pair > 0)
539+
if (it != NULL && it->s != NULL && it->current_bucket < it->s->capacity && it->current_pair > 0)
548540
{
549541
return it->s->buckets[it->current_bucket].pairs[it->current_pair - 1].key;
550542
}
@@ -554,7 +546,7 @@ set_key set_iterator_key(set_iterator it)
554546

555547
set_value set_iterator_value(set_iterator it)
556548
{
557-
if (it != NULL && it->current_bucket < it->s->capacity && it->current_pair > 0)
549+
if (it != NULL && it->s != NULL && it->current_bucket < it->s->capacity && it->current_pair > 0)
558550
{
559551
return it->s->buckets[it->current_bucket].pairs[it->current_pair - 1].value;
560552
}
@@ -564,7 +556,7 @@ set_value set_iterator_value(set_iterator it)
564556

565557
void set_iterator_next(set_iterator it)
566558
{
567-
if (it != NULL)
559+
if (it != NULL && it->s != NULL)
568560
{
569561
for (; it->current_bucket < it->s->capacity; ++it->current_bucket)
570562
{
@@ -590,16 +582,12 @@ void set_iterator_next(set_iterator it)
590582
}
591583
}
592584

593-
int set_iterator_end(set_iterator *it)
585+
int set_iterator_end(set_iterator it)
594586
{
595-
if (it != NULL && *it != NULL)
587+
if (it != NULL && it->s != NULL)
596588
{
597-
if ((*it)->current_bucket >= (*it)->s->capacity)
589+
if (it->current_bucket >= it->s->capacity)
598590
{
599-
free(*it);
600-
601-
*it = NULL;
602-
603591
return 0;
604592
}
605593

source/adt/source/adt_trie.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,11 @@ void trie_node_iterate(trie t, trie_node n, trie_cb_iterate iterate_cb, trie_cb_
524524

525525
if (back->childs != NULL)
526526
{
527-
set_iterator it;
527+
struct set_iterator_type it;
528528

529-
for (it = set_iterator_begin(back->childs); set_iterator_end(&it) > 0; set_iterator_next(it))
529+
for (set_iterator_begin(&it, back->childs); set_iterator_end(&it) > 0; set_iterator_next(&it))
530530
{
531-
trie_node_ref ref_node = set_iterator_value(it);
531+
trie_node_ref ref_node = set_iterator_value(&it);
532532

533533
trie_node current_node = &t->node_list[ref_node->index];
534534

@@ -610,11 +610,11 @@ int trie_node_clear(trie t, trie_node n)
610610

611611
if (back->childs != NULL)
612612
{
613-
set_iterator it;
613+
struct set_iterator_type it;
614614

615-
for (it = set_iterator_begin(back->childs); set_iterator_end(&it) > 0; set_iterator_next(it))
615+
for (set_iterator_begin(&it, back->childs); set_iterator_end(&it) > 0; set_iterator_next(&it))
616616
{
617-
trie_node_ref ref_node = set_iterator_value(it);
617+
trie_node_ref ref_node = set_iterator_value(&it);
618618

619619
trie_node current_node = &t->node_list[ref_node->index];
620620

@@ -682,14 +682,13 @@ trie_node trie_node_find(trie t, trie_key key)
682682
if (back_ptr != NULL && *back_ptr != NULL)
683683
{
684684
trie_node back = *back_ptr;
685-
686-
set_iterator it = NULL;
685+
struct set_iterator_type it;
687686

688687
if (back->childs != NULL)
689688
{
690-
for (it = set_iterator_begin(back->childs); set_iterator_end(&it) > 0; set_iterator_next(it))
689+
for (set_iterator_begin(&it, back->childs); set_iterator_end(&it) > 0; set_iterator_next(&it))
691690
{
692-
trie_node_ref ref_node = set_iterator_value(it);
691+
trie_node_ref ref_node = set_iterator_value(&it);
693692

694693
trie_node current_node = &t->node_list[ref_node->index];
695694

@@ -699,9 +698,10 @@ trie_node trie_node_find(trie t, trie_key key)
699698

700699
if (back->key != NULL && t->compare_cb(back->key, key) == 0)
701700
{
701+
/* TODO: it may be un-initialized here */
702702
while (set_iterator_end(&it) > 0)
703703
{
704-
set_iterator_next(it);
704+
set_iterator_next(&it);
705705
}
706706

707707
vector_destroy(node_stack);

0 commit comments

Comments
 (0)