Skip to main content

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

Code: 0x00 (TN_SYSCALL_CODE_SET_ANONYMOUS_SEGMENT_SZ)

C SDK Function

ulong tsys_set_anonymous_segment_sz(void* addr);

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

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 (-20) - Invalid segment type or index
  • TN_VM_ERR_SYSCALL_INVALID_SEGMENT_SIZE (-27) - Invalid segment size (not page-aligned)
  • TN_VM_ERR_SYSCALL_INSUFFICIENT_PAGES (-25) - Not enough free pages for expansion
  • TN_VM_ERR_SYSCALL_UNFREEABLE_PAGE (-28) - Cannot free pages when shrinking

Resource Consumption

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

  • 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_SZ chunks

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

  • 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

#include "tn_sdk_syscall.h"

// Set the heap segment (index 0) to 8192 bytes
void* segment_addr = TN_VM_ADDR(TN_VM_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
}