@@ -154,6 +154,79 @@ fossil_sys_memory_t fossil_sys_memory_resize(fossil_sys_memory_t ptr, size_t old
154154 */
155155bool fossil_sys_memory_is_valid (const fossil_sys_memory_t ptr);
156156
157+ /* *
158+ * @brief Fill memory with a repeating pattern.
159+ *
160+ * Useful for debugging or initializing buffers to known patterns.
161+ *
162+ * @param ptr A pointer to the memory to fill.
163+ * @param pattern Pointer to the pattern to repeat.
164+ * @param pattern_size Size of the pattern in bytes.
165+ * @param total_size Total number of bytes to fill.
166+ * @return A pointer to the memory.
167+ */
168+ fossil_sys_memory_t fossil_sys_memory_fill (fossil_sys_memory_t ptr,
169+ const void *pattern,
170+ size_t pattern_size,
171+ size_t total_size);
172+
173+ /* *
174+ * @brief Securely zero memory.
175+ *
176+ * Unlike fossil_sys_memory_zero, this function prevents
177+ * the compiler from optimizing out the zeroing operation.
178+ *
179+ * @param ptr A pointer to the memory to zero.
180+ * @param size The size of the memory to zero.
181+ */
182+ void fossil_sys_memory_secure_zero (fossil_sys_memory_t ptr, size_t size);
183+
184+ /* *
185+ * @brief Swap contents of two memory regions.
186+ *
187+ * Swaps memory between two buffers of the same size.
188+ *
189+ * @param a Pointer to the first buffer.
190+ * @param b Pointer to the second buffer.
191+ * @param size Number of bytes to swap.
192+ * @throws Error message and exits if pointers are NULL or sizes are zero.
193+ */
194+ void fossil_sys_memory_swap (fossil_sys_memory_t a, fossil_sys_memory_t b, size_t size);
195+
196+ /* *
197+ * @brief Search memory for a byte value.
198+ *
199+ * Scans memory for the first occurrence of the given byte.
200+ *
201+ * @param ptr Pointer to the memory to search.
202+ * @param value Byte value to search for.
203+ * @param size Size of the memory to search.
204+ * @return Pointer to the first occurrence of the value, or NULL if not found.
205+ */
206+ void *fossil_sys_memory_find (const fossil_sys_memory_t ptr, uint8_t value, size_t size);
207+
208+ /* *
209+ * @brief Duplicate a NULL-terminated string using memory API.
210+ *
211+ * Similar to strdup, but uses fossil_sys_memory_alloc under the hood.
212+ *
213+ * @param str The string to duplicate.
214+ * @return A pointer to the duplicated string.
215+ * @throws Error message and exits if allocation fails or str is NULL.
216+ */
217+ char *fossil_sys_memory_strdup (const char *str);
218+
219+ /* *
220+ * @brief Get memory usage statistics.
221+ *
222+ * Returns current allocation count and optionally total allocated bytes.
223+ * (Useful for leak detection in debug builds.)
224+ *
225+ * @param out_allocs Pointer to receive allocation count (can be NULL).
226+ * @param out_bytes Pointer to receive total allocated bytes (can be NULL).
227+ */
228+ void fossil_sys_memory_stats (size_t *out_allocs, size_t *out_bytes);
229+
157230#ifdef __cplusplus
158231}
159232
@@ -313,9 +386,76 @@ namespace fossil {
313386 * @param ptr A pointer to the memory.
314387 * @return true if the memory is valid, false otherwise.
315388 */
316- static bool isValid (const fossil_sys_memory_t ptr) {
389+ static bool is_valid (const fossil_sys_memory_t ptr) {
317390 return fossil_sys_memory_is_valid (ptr);
318391 }
392+
393+ /* *
394+ * Fill memory with a repeating pattern.
395+ *
396+ * @param ptr A pointer to the memory to fill.
397+ * @param pattern Pointer to the pattern to repeat.
398+ * @param pattern_size Size of the pattern in bytes.
399+ * @param total_size Total number of bytes to fill.
400+ * @return A pointer to the memory.
401+ */
402+ static fossil_sys_memory_t fill (fossil_sys_memory_t ptr, const void *pattern, size_t pattern_size, size_t total_size) {
403+ return fossil_sys_memory_fill (ptr, pattern, pattern_size, total_size);
404+ }
405+
406+ /* *
407+ * Securely zero memory.
408+ *
409+ * @param ptr A pointer to the memory to zero.
410+ * @param size The size of the memory to zero.
411+ */
412+ static void secure_zero (fossil_sys_memory_t ptr, size_t size) {
413+ fossil_sys_memory_secure_zero (ptr, size);
414+ }
415+
416+ /* *
417+ * Swap contents of two memory regions.
418+ *
419+ * @param a Pointer to the first buffer.
420+ * @param b Pointer to the second buffer.
421+ * @param size Number of bytes to swap.
422+ */
423+ static void swap (fossil_sys_memory_t a, fossil_sys_memory_t b, size_t size) {
424+ fossil_sys_memory_swap (a, b, size);
425+ }
426+
427+ /* *
428+ * Search memory for a byte value.
429+ *
430+ * @param ptr Pointer to the memory to search.
431+ * @param value Byte value to search for.
432+ * @param size Size of the memory to search.
433+ * @return Pointer to the first occurrence of the value, or NULL if not found.
434+ */
435+ static void *find (const fossil_sys_memory_t ptr, uint8_t value, size_t size) {
436+ return fossil_sys_memory_find (ptr, value, size);
437+ }
438+
439+ /* *
440+ * Duplicate a NULL-terminated string using memory API.
441+ *
442+ * @param str The string to duplicate.
443+ * @return A pointer to the duplicated string.
444+ */
445+ static char *strdup (const char *str) {
446+ return fossil_sys_memory_strdup (str);
447+ }
448+
449+ /* *
450+ * Get memory usage statistics.
451+ *
452+ * @param out_allocs Pointer to receive allocation count (can be NULL).
453+ * @param out_bytes Pointer to receive total allocated bytes (can be NULL).
454+ */
455+ static void stats (size_t *out_allocs, size_t *out_bytes) {
456+ fossil_sys_memory_stats (out_allocs, out_bytes);
457+ }
458+
319459 };
320460
321461 }
0 commit comments