set_anonymous_segment_sz
Overview
Section titled “Overview”The set_anonymous_segment_sz syscall sets the size of an anonymous memory segment to a specific value. This syscall is used for precise memory management when you need to set an exact segment size.
Syscall Code
Section titled “Syscall Code”Code: 0x00 (TN_SYSCALL_CODE_SET_ANONYMOUS_SEGMENT_SZ)
C SDK Function
Section titled “C SDK Function”ulong tsys_set_anonymous_segment_sz(void* addr);Arguments
Section titled “Arguments”addr · void · required*
Virtual address that encodes the segment type, index, and desired size offset. The size is determined by the offset in the address.
Return Value
Section titled “Return Value”Returns a syscall result code:
Success · ulong
TN_VM_SYSCALL_SUCCESS(0) - Operation completed successfully
Error Codes · ulong
TN_VM_ERR_SYSCALL_INVALID_SEGMENT_ID(-21) - Invalid segment type or indexTN_VM_ERR_SYSCALL_INVALID_SEGMENT_SIZE(-28) - Invalid segment size (not page-aligned)TN_VM_ERR_SYSCALL_INSUFFICIENT_PAGES(-26) - Not enough free pages for expansionTN_VM_ERR_SYSCALL_UNFREEABLE_PAGE(-29) - Cannot free pages when shrinking
Resource Consumption
Section titled “Resource Consumption”Compute Units
Section titled “Compute Units”- Base cost:
TN_VM_SYSCALL_BASE_COST(512 units) - Additional cost: Variable based on size change
- Expansion: Additional units equal to the number of bytes increased
- Shrinking: No additional cost beyond base
- Total formula:
base_cost + max(0, size_increase)
Memory Pages
Section titled “Memory Pages”- Page allocation: Required pages are allocated from the transaction’s page pool
- Page deallocation: Freed pages are returned to the transaction’s page pool
- Page size: Operations work in
TN_COW_TABLE_PAGE_SZchunks
Side Effects
Section titled “Side Effects”- Memory allocation/deallocation: Pages are allocated when expanding or freed when shrinking the segment
- Segment state: Updates the segment size and page mappings
Usage Notes
Section titled “Usage Notes”- The segment size must be page-aligned (multiple of page size)
- For stack segments, the size is calculated as
(0xFFFFFF - offset + 1) - For heap segments, the size is equal to the offset
- Only works with anonymous data segments (heap and stack types)
- Pages are allocated from the transaction’s page pool
Example
Section titled “Example”#include <thru-sdk/c/tn_sdk_syscall.h>
// Set the heap segment (index 0) to 8192 bytesvoid* segment_addr = TSDK_ADDR(TSDK_SEG_TYPE_HEAP, 0, 8192);ulong result = tsys_set_anonymous_segment_sz(segment_addr);
if (result == TN_VM_SYSCALL_SUCCESS) { // Segment size set successfully // Segment now has exactly 8192 bytes allocated}