---
title: set_anonymous_segment_sz
description: Sets the size of an anonymous memory segment to a specific value
source_url:
  html: https://thru.org/docs/spec/vm/syscalls/set_anonymous_segment_sz/
  md: https://thru.org/docs/spec/vm/syscalls/set_anonymous_segment_sz.md
---

# set_anonymous_segment_sz

## 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

```c
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` (-21) - Invalid segment type or index
- `TN_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 expansion
- `TN_VM_ERR_SYSCALL_UNFREEABLE_PAGE` (-29) - 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

```c
#include <thru-sdk/c/tn_sdk_syscall.h>

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