Skip to content

Commit 8e4d03f

Browse files
committed
git subrepo pull lib/ssm
subrepo: subdir: "lib/ssm" merged: "a9bf527" upstream: origin: "ssm-runtime" branch: "main" commit: "a9bf527" git-subrepo: version: "0.4.3" origin: "https://github.com/ingydotnet/git-subrepo" commit: "2f68596"
1 parent bfbde4f commit 8e4d03f

File tree

14 files changed

+620
-79
lines changed

14 files changed

+620
-79
lines changed

lib/ssm/.gitrepo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[subrepo]
77
remote = ssm-runtime
88
branch = main
9-
commit = a3eb8f36d0989878f3485f47b7913a222552ca78
10-
parent = ce89861d65a11ff7f5a94b89bf2cf8ac3f973db8
9+
commit = a9bf5272efed87994389613dd29e8f99bb7465c5
10+
parent = bfbde4f3b1727f9a6ffe768391f10c50e3f2fbdb
1111
method = merge
1212
cmdver = 0.4.3

lib/ssm/Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ $(info PLATFORM is $(PLATFORM))
6565
COV_TGT := $(BUILD_DIR)/coverage.xml
6666

6767
CC = $(MAKE_CC)
68-
CFLAGS += -g -I$(INC_DIR) -O -Wall -pedantic
68+
CFLAGS += -g -I$(INC_DIR) -O -Wall -pedantic $(EXTRA_CFLAGS)
6969

7070
ifeq ($(PLATFORM),simulation)
7171
# enforce c99 for pure testing, but don't enforce it for other platforms
@@ -74,7 +74,8 @@ endif
7474

7575
ifeq ($(PLATFORM),simulation)
7676
# Check whether valgrind is available.
77-
ifeq ($(shell command -v valgrind),)
77+
# ifeq (0,0)
78+
ifeq ($(shell which valgrind),)
7879
$(info # Valgrind is not available; compiling without it.)
7980
else
8081
# If available, we try to #include <valgrind/valgrind.h>, which we use to
@@ -87,7 +88,7 @@ CFLAGS += -DUSE_VALGRIND
8788
endif
8889
endif
8990

90-
TEST_CFLAGS = $(CFLAGS) -g -DSSM_DEBUG --coverage
91+
TEST_CFLAGS = $(CFLAGS) -g -DSSM_DEBUG --coverage -DCONFIG_MEM_STATS
9192

9293
LD = $(MAKE_LD)
9394
LDFLAGS = -L$(BUILD_DIR)
@@ -164,5 +165,7 @@ help:
164165
@echo
165166
@echo "Available example targets:" $(EXE_TGT)
166167
@echo "Available test targets:" $(TEST_TGT)
168+
@echo
169+
@echo "Specify, e.g., CONFIGS=\"CONFIG_MEM_STATS SSM_DEBUG\" to define additional compilation flags"
167170

168171
.PHONY: $(PHONY)

lib/ssm/doc/Doxyfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ INCLUDE_FILE_PATTERNS =
273273

274274
PREDEFINED += __GNUC__
275275
PREDEFINED += UINTPTR_MAX=0xffffffffu
276+
PREDEFINED += CONFIG_MEM_STATS
277+
PREDEFINED += CONFIG_MEM_TRACE
278+
PREDEFINED += CONFIG_MALLOC_HEAP
276279

277280
SKIP_FUNCTION_MACROS = YES
278281

lib/ssm/doc/mem.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,12 @@ The allocator's memory pools may be configured using the #SSM_MEM_POOL_MIN,
5050
preprocessor constants.
5151
The default values produce 4 memory pools, of sizes 16B, 64B, 256B, and 1024B,
5252
and a memory page size of 4096B.
53+
54+
The runtime recognizes several compile time configuration macros that help with
55+
profiling and debugging. They are:
56+
57+
- CONFIG_MEM_TRACE: log information any time something is allocated or freed.
58+
- CONFIG_MEM_STATS: collect statistics and report about memory pool usage.
59+
- CONFIG_MALLOC_HEAP: bypass the allocator entirely and allocate everything
60+
using malloc(), so that memory accesses can be tracked with Valgrind.
61+

lib/ssm/include/ssm-internal.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,46 @@ void ssm_mem_destroy(void (*free_page_handler)(void *));
241241
#define SSM_MEM_PAGE_SIZE SSM_MEM_POOL_SIZE(SSM_MEM_POOL_COUNT)
242242
#endif
243243

244+
#ifdef CONFIG_MEM_STATS
245+
246+
/** @brief Statistics for a heap page pool; used in #ssm_mem_statistics_t.
247+
*
248+
* @note Define CONFIG_MEM_STATS to enable this.
249+
*/
250+
typedef struct ssm_mem_statistics_pool {
251+
size_t block_size; /**< Size of this pool's blocks */
252+
size_t pages_allocated; /**< Number of pages allocated to this pool */
253+
size_t free_list_length; /**< Length of the free list */
254+
} ssm_mem_statistics_pool_t;
255+
256+
/** @brief Statistics for the heap; filled with #ssm_mem_statistics_collect().
257+
*
258+
* A collection of satistics collected by #ssm_mem_statistics_collect()
259+
* and designed to be printed by a function you supply.
260+
*
261+
* @note Define CONFIG_MEM_STATS to enable this.
262+
*/
263+
typedef struct ssm_mem_statistics {
264+
size_t sizeof_ssm_mm; /**< Size of per-object memory management header */
265+
size_t page_size; /**< Bytes in a memory page */
266+
size_t pages_allocated; /**< Number of pages that have been allocated */
267+
size_t objects_allocated; /**< Total number of allocated objects */
268+
size_t objects_freed; /**< Total number of object free events */
269+
size_t live_objects; /**< Number of live objects **/
270+
size_t pool_count; /**< Number of memory pools */
271+
ssm_mem_statistics_pool_t pool[32]; /**< Size of the blocks in each pool */
272+
} ssm_mem_statistics_t;
273+
274+
/** @brief Collect and return statistics about the heap.
275+
*
276+
* @note Define CONFIG_MEM_STATS to enable this.
277+
*
278+
* @param stats non-NULL pointer to a #ssm_mem_statistics_t to be filled in
279+
*/
280+
void ssm_mem_statistics_collect(ssm_mem_statistics_t *stats);
281+
282+
#endif /* CONFIG_MEM_STATS */
283+
244284
/** @} */
245285

246286
#ifdef USE_VALGRIND

lib/ssm/include/ssm.h

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#include <stdint.h> /* For uint16_t, UINT64_MAX etc. */
1515
#include <stdlib.h> /* For size_t */
1616

17+
#ifdef CONFIG_MEM_TRACE
18+
#include <stdio.h>
19+
#endif
20+
1721
/**
1822
* @addtogroup error
1923
* @{
@@ -169,8 +173,18 @@ typedef struct ssm_trigger {
169173
*
170174
* @throws SSM_EXHAUSTED_MEMORY out of memory.
171175
*/
172-
ssm_act_t *ssm_enter(size_t size, ssm_stepf_t step, ssm_act_t *parent,
173-
ssm_priority_t priority, ssm_depth_t depth);
176+
ssm_act_t *ssm_enter_int(size_t size, ssm_stepf_t step, ssm_act_t *parent,
177+
ssm_priority_t priority, ssm_depth_t depth);
178+
179+
#ifdef CONFIG_MEM_TRACE
180+
#define ssm_enter(si, st, pa, pr, de) \
181+
(fprintf(stderr, "%s:%d:ssm_enter(%lu,_,_,_,_,_)\n", __FILE__, __LINE__, \
182+
(si)), \
183+
ssm_enter_int((si), (st), (pa), (pr), (de)))
184+
#else
185+
#define ssm_enter(si, st, pa, pr, de) \
186+
ssm_enter_int((si), (st), (pa), (pr), (de))
187+
#endif
174188

175189
/** @brief Destroy the activation record of a routine before leaving.
176190
*
@@ -426,8 +440,17 @@ ssm_time_t ssm_now(void);
426440
*
427441
* @param time what the heap-allocated @a time field is initialized to.
428442
* @returns #ssm_value_t pointing to the heap-allocated #ssm_time.
443+
*
444+
* ssm_value_t ssm_new_time(ssm_time_t time)
429445
*/
430-
ssm_value_t ssm_new_time(ssm_time_t time);
446+
extern ssm_value_t ssm_new_time_int(ssm_time_t time);
447+
#ifdef CONFIG_MEM_TRACE
448+
#define ssm_new_time(t) \
449+
(fprintf(stderr, "%s:%d:ssm_new_time()\n", __FILE__, __LINE__), \
450+
ssm_new_time_int(t))
451+
#else
452+
#define ssm_new_time(t) ssm_new_time_int(t)
453+
#endif
431454

432455
/** @brief Read the heap-allocated time pointed to by an #ssm_value_t.
433456
*
@@ -483,7 +506,16 @@ struct ssm_adt1 {
483506
* @param tag the tag of the ADT object, stored in the #ssm_mm header.
484507
* @returns #ssm_value_t poining to the ADT object on the heap.
485508
*/
486-
ssm_value_t ssm_new_adt(uint8_t field_count, uint8_t tag);
509+
extern ssm_value_t ssm_new_adt_int(uint8_t field_count, uint8_t tag);
510+
511+
#ifdef CONFIG_MEM_TRACE
512+
#define ssm_new_adt(fc, tag) \
513+
(fprintf(stderr, "%s:%d:ssm_new_adt(%d, %d)\n", __FILE__, __LINE__, (fc), \
514+
(tag)), \
515+
ssm_new_adt_int((fc), (tag)))
516+
#else
517+
#define ssm_new_adt(fc, tag) ssm_new_adt_int((fc), (tag))
518+
#endif
487519

488520
/** @brief Access the field of an ADT object.
489521
*
@@ -572,7 +604,14 @@ typedef struct ssm_sv {
572604
* @param val the initial value of the scheduled variable.
573605
* @returns #ssm_value_t pointing to the #ssm_sv on the heap.
574606
*/
575-
ssm_value_t ssm_new_sv(ssm_value_t val);
607+
extern ssm_value_t ssm_new_sv_int(ssm_value_t val);
608+
#ifdef CONFIG_MEM_TRACE
609+
#define ssm_new_sv(v) \
610+
(fprintf(stderr, "%s:%d:ssm_new_sv()\n", __FILE__, __LINE__), \
611+
ssm_new_sv_int(v))
612+
#else
613+
#define ssm_new_sv(v) ssm_new_sv_int(v)
614+
#endif
576615

577616
/** @brief Retrieve #ssm_sv pointer pointed to by an #ssm_value_t.
578617
*
@@ -837,7 +876,15 @@ struct ssm_closure1 {
837876
* @param arg_cap the number of arguments the closure should accommodate.
838877
* @returns a value pointing to the heap-allocated closure.
839878
*/
840-
ssm_value_t ssm_new_closure(ssm_func_t f, uint8_t arg_cap);
879+
extern ssm_value_t ssm_new_closure_int(ssm_func_t f, uint8_t arg_cap);
880+
#ifdef CONFIG_MEM_TRACE
881+
#define ssm_new_closure(f, args) \
882+
(fprintf(stderr, "%s:%d:ssm_new_closure(_, %d)\n", __FILE__, __LINE__, \
883+
(args)), \
884+
ssm_new_closure_int((f), (args)))
885+
#else
886+
#define ssm_new_closure(f, args) ssm_new_closure_int((f), (args))
887+
#endif
841888

842889
/** @brief Create a copy of a closure.
843890
*
@@ -999,7 +1046,16 @@ struct ssm_array1 {
9991046
* @param count number of #ssm_value_t elements to be stored in the array.
10001047
* @returns #ssm_value_t pointing to heap-allocated array.
10011048
*/
1002-
ssm_value_t ssm_new_array(uint16_t count);
1049+
1050+
extern ssm_value_t ssm_new_array_int(uint16_t count);
1051+
#ifdef CONFIG_MEM_TRACE
1052+
#define ssm_new_array(c) \
1053+
(fprintf(stderr,"%s:%d:ssm_new_array(%d)\n", \
1054+
__FILE__, __LINE__, (c)), \
1055+
ssm_new_array_int(c)
1056+
#else
1057+
#define ssm_new_array(c) ssm_new_array_int(c)
1058+
#endif
10031059

10041060
/** @} */
10051061

@@ -1048,7 +1104,8 @@ struct ssm_blob1 {
10481104
* @param size scaled size of the blob's payload.
10491105
* @returns size that a blob of @a size payload occupies in the heap.
10501106
*/
1051-
#define ssm_blob_size(size) (sizeof(struct ssm_blob1) + (size)-SSM_BLOB_SIZE_SCALE)
1107+
#define ssm_blob_size(size) \
1108+
(sizeof(struct ssm_blob1) - SSM_BLOB_SIZE_SCALE + (size))
10521109

10531110
/** @brief Compute the size a blob in the heap.
10541111
*
@@ -1067,7 +1124,14 @@ struct ssm_blob1 {
10671124
* @param size size of the payload to the allocated.
10681125
* @returns #ssm_value_t pointing to heap-allocated blob.
10691126
*/
1070-
ssm_value_t ssm_new_blob(uint16_t size);
1127+
extern ssm_value_t ssm_new_blob_int(uint16_t size);
1128+
#ifdef CONFIG_MEM_TRACE
1129+
#define ssm_new_blob(s) \
1130+
(fprintf(stderr, "%s:%d:ssm_new_blob(%lu)\n", __FILE__, __LINE__, (size)), \
1131+
ssm_new_blob_int(size))
1132+
#else
1133+
#define ssm_new_blob(s) ssm_new_blob_int(s)
1134+
#endif
10711135

10721136
/** @} */
10731137

lib/ssm/platform/posix/src/posix-io.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ static void step_stdout_handler(ssm_act_t *act) {
1717
case 1:;
1818
char c = ssm_unmarshal(ssm_deref(cont->ssm_stdout));
1919
if (!c)
20-
break;
20+
return;
2121
write(1, &c, 1);
2222
return;
23+
case 2:; // Magical "terminate self" code
24+
break;
2325
}
26+
27+
ssm_desensitize(
28+
&cont->trigger); // sensitize added a pointer to the activation record
29+
ssm_drop(cont->ssm_stdout); // Caller drop this argument
2430
ssm_leave(&cont->act, sizeof(stdout_handler_act_t));
2531
}
2632

0 commit comments

Comments
 (0)