MemCopy
Description
Copy memory from source to destination. A zero byte count returns dst without reading either pointer.
Parameters
| Name | Direction | Description |
|---|---|---|
dst |
out | Destination memory region. |
src |
in | Source memory region. |
n |
in | Number of bytes to copy. |
Success
Returns destination pointer.
Failure
Aborts via LOG_FATAL when n > 0 and either dst or src is NULL. Behaviour is undefined when the regions overlap; use MemMove for that case.
Usage example (Cross-references)
Usage examples (Cross-references)
__attribute__((used)) void *memcpy(void *dst, const void *src, freestanding_size_t n) {
MemCopy(dst, src, (size)n);
return dst;
}- In
Memory.c:31:
}
void *MemCopy(void *dst, const void *src, size n) {
if (n == 0) {
return dst;- In
Zstr.c:166:
}
MemCopy(new_str, src, len);
new_str[len] = '\0';
return new_str;- In
Io.c:511:
ok = false;
} else if (StrLen(&tmp)) {
MemCopy(StrBegin(o) + offset, StrBegin(&tmp), StrLen(&tmp));
}
}- In
Io.c:621:
StrInitStack(spec_buf, 32) {
char *data = StrBegin(&spec_buf);
MemCopy(data, start, spec_len);
data[spec_len] = '\0';
StrResize(&spec_buf, (size)spec_len);- In
Io.c:1084:
ok = false;
} else if (StrLen(&tmp)) {
MemCopy(BufData(out) + offset, StrBegin(&tmp), StrLen(&tmp));
}
}- In
Io.c:2015:
if (fmt_info->flags & FMT_FLAG_CHAR) {
u64 bits;
MemCopy(&bits, v, sizeof(bits));
return write_int_as_chars(o, fmt_info->flags, bits, 8);
}- In
Io.c:2096:
if (fmt_info->flags & FMT_FLAG_CHAR) {
u32 bits;
MemCopy(&bits, v, sizeof(bits));
return write_int_as_chars(o, fmt_info->flags, bits, 4);
}- In
ArgParse.c:501:
}
char *data = StrBegin(&flagbuf);
MemCopy(data, tok, n);
data[n] = '\0';
StrResize(&flagbuf, (size)n);- In
Page.c:204:
}
if (*arr_p) {
MemCopy(new_table, *arr_p, (size)*len_p * sizeof(PageEntry));
os_page_unmap(&page->base, *arr_p, *bytes_p);
}- In
Page.c:464:
}
size copy_bytes = old_rounded < new_rounded ? old_rounded : new_rounded;
MemCopy(fresh, ptr, copy_bytes);
(void)page_allocator_deallocate(self, ptr);
return fresh;- In
Heap.c:416:
return false;
if (*arr && len) {
MemCopy(fresh, *arr, (size)len * sizeof(HeapPageXL));
}
if (*arr) {- In
Heap.c:500:
return false;
if (heap->recycle && heap->recycle_len) {
MemCopy(fresh, heap->recycle, (size)heap->recycle_len * sizeof(void *));
}
if (heap->recycle) {- In
Heap.c:1014:
return NULL;
size copy_bytes = cur < new_size ? cur : new_size;
MemCopy(fresh, ptr, copy_bytes);
(void)heap_allocator_deallocate(self, ptr);
return fresh;- In
Debug.c:365:
entry.requested_size = live_rec->requested_size;
entry.alloc_trace_n = live_rec->alloc_trace_n;
MemCopy(entry.alloc_trace, live_rec->alloc_trace, (size)live_rec->alloc_trace_n * sizeof(StackFrame));
entry.free_trace_n = 0;
if (self->config.capture_traces && self->config.trace_depth > 0) {- In
Debug.c:446:
return NULL;
size copy = old_requested < new_size ? old_requested : new_size;
MemCopy(fresh, ptr, copy);
debug_allocator_deallocate(self, ptr);
return fresh;- In
Arena.c:254:
return NULL;
}
MemCopy(fresh, ptr, old_padded < new_size ? old_padded : new_size);
#if FEATURE_ALLOC_STATS
// The user is abandoning the old `ptr` in favour of `fresh`. The
- In
Slab.c:158:
if (slab->slabs && old_cap) {
MemCopy(new_slabs, slab->slabs, (size)old_cap * sizeof(void *));
MemCopy(new_bitmaps, slab->bitmaps, (size)old_cap * (size)slab->bitmap_words_per_slab * sizeof(u64));
os_page_unmap(&slab->base, slab->slabs, os_page_round_up((size)old_cap * sizeof(void *)));- In
Slab.c:159:
if (slab->slabs && old_cap) {
MemCopy(new_slabs, slab->slabs, (size)old_cap * sizeof(void *));
MemCopy(new_bitmaps, slab->bitmaps, (size)old_cap * (size)slab->bitmap_words_per_slab * sizeof(u64));
os_page_unmap(&slab->base, slab->slabs, os_page_round_up((size)old_cap * sizeof(void *)));
os_page_unmap(- In
Graph.c:135:
}
MemCopy(dst, src, item_size);
return true;
}- In
List.c:58:
}
} else {
MemCopy(new_node->data, item_data, item_size);
}- In
List.c:122:
GenericListNode *node = node_at_list(list, item_size, start);
for (u64 c = 0; (c < count) && node; c++) {
MemCopy((u8 *)removed_data + c * item_size, node->data, item_size);
MemSet(node->data, 0, item_size);- In
List.c:202:
node = list->head;
for (index = 0; node && index < item_count; index++) {
MemCopy((u8 *)data + index * item_size, node->data, item_size);
node = node->next;
}- In
List.c:210:
node = list->head;
for (index = 0; node && index < item_count; index++) {
MemCopy(node->data, (u8 *)data + index * item_size, item_size);
node = node->next;
}- In
Vec.c:263:
inserted_count++;
} else {
MemCopy(vec_ptr_at(vec, idx + i, item_size), item_data + i * item_size, item_size);
}
}- In
Vec.c:337:
inserted_count++;
} else {
MemCopy(vec_ptr_at(vec, idx + i, item_size), item_data + i * item_size, item_size);
}
}- In
Vec.c:364:
// bytes live for both paths and lets the slide be a single MemMove.
if (removed_data) {
MemCopy(removed_data, vec_ptr_at(vec, start, item_size), count * vec_aligned_size(vec, item_size));
} else {
if (vec->copy_deinit) {- In
Vec.c:406:
if (removed_data) {
MemCopy(removed_data, vec_ptr_at(vec, start, item_size), count * vec_aligned_size(vec, item_size));
} else {
if (vec->copy_deinit) {- In
Str.c:105:
}
MemCopy(out->data, cstr, len);
out->data[len] = 0;
out->length = len;- In
Map.c:243:
}
} else {
MemCopy(dst_key, key, key_size);
}- In
Map.c:258:
}
} else {
MemCopy(dst_val, value, value_size);
}- In
Map.c:389:
}
MemCopy(map_entry_ptr(map, entry_size, insert_idx), entry, entry_size);
map->states[insert_idx] = MAP_SLOT_OCCUPIED;
map->length += 1;- In
Map.c:840:
value_ptr = map_get_value_ptr(map, key, entry_size, key_offset, key_size, value_offset, hash_offset);
if (value_ptr) {
MemCopy(out_value, value_ptr, value_size);
} else {
MemCopy(out_value, default_value, value_size);- In
Map.c:842:
MemCopy(out_value, value_ptr, value_size);
} else {
MemCopy(out_value, default_value, value_size);
}- In
Map.c:1149:
if (map->value_copy_init) {
MemCopy(dst_value, temp_value, value_size);
AllocatorFree(map->allocator, temp_value);
} else {- In
Map.c:1152:
AllocatorFree(map->allocator, temp_value);
} else {
MemCopy(dst_value, value, value_size);
}- In
Socket.c:334:
return false;
}
MemCopy(host_out, spec + 1, host_len);
host_out[host_len] = '\0';
*port_out = close + 2;- In
Socket.c:354:
return false;
}
MemCopy(host_out, spec, host_len);
host_out[host_len] = '\0';
*port_out = colon + 1;- In
Socket.c:371:
len = (u32)SOCKET_ADDR_MAX_SIZE;
}
MemCopy(out->raw, sa, (size)len);
out->length = len;
out->family = af_to_socket_family((i32)sa->sa_family);- In
Socket.c:415:
sa->sin_family = AF_INET;
sa->sin_port = FROM_BIG_ENDIAN2(port);
MemCopy(&sa->sin_addr.s_addr, v4, 4);
out->length = (u32)sizeof(struct sockaddr_in);
out->family = SOCKET_FAMILY_INET;- In
Socket.c:428:
// Windows' IN6_ADDR defines `#define s6_addr u.Byte`, so this
// works the same on both platforms.
MemCopy(sa->sin6_addr.s6_addr, v6, 16);
out->length = (u32)sizeof(struct sockaddr_in6);
out->family = SOCKET_FAMILY_INET6;- In
Dir.c:84:
}
char *data = StrBegin(&search_path);
MemCopy(data, path, path_len);
data[path_len] = '\\';
data[path_len + 1] = '*';- In
Dir.c:480:
StrInitStack(buf, 4096) {
char *data = StrBegin(&buf);
MemCopy(data, path, n);
data[n] = 0;
StrResize(&buf, n);- In
Dns.c:63:
sa->sin_family = AF_INET;
sa->sin_port = FROM_BIG_ENDIAN2(port);
MemCopy(&sa->sin_addr.s_addr, ip, 4);
a.length = (u32)sizeof(struct sockaddr_in);
a.family = SOCKET_FAMILY_INET;- In
Dns.c:74:
sa->sin6_family = AF_INET6;
sa->sin6_port = FROM_BIG_ENDIAN2(port);
MemCopy(sa->sin6_addr.s6_addr, ip, 16);
a.length = (u32)sizeof(struct sockaddr_in6);
a.family = SOCKET_FAMILY_INET6;- In
Dns.c:220:
ascii_lower((u8 *)StrBegin(&e.name), StrLen(&e.name));
if (got_v4) {
MemCopy(e.ip, v4, 4);
e.is_ipv6 = false;
} else {- In
Dns.c:223:
e.is_ipv6 = false;
} else {
MemCopy(e.ip, v6, 16);
e.is_ipv6 = true;
}- In
MachO.c:185:
// plus the 16-byte segname plus the body.
IterMustMove(cmd, 8); // skip cmd(4) + cmdsize(4) prefix
MemCopy(seg.name, IterDataAt(cmd, IterIndex(cmd)), 16);
seg.name[16] = '\0';
IterMustMove(cmd, 16);- In
MachO.c:222:
MachoSection sec;
MemSet(&sec, 0, sizeof(sec));
MemCopy(sec.section, IterDataAt(&sec_it, IterIndex(&sec_it)), 16);
sec.section[16] = '\0';
IterMustMove(&sec_it, 16);- In
MachO.c:225:
sec.section[16] = '\0';
IterMustMove(&sec_it, 16);
MemCopy(sec.segment, IterDataAt(&sec_it, IterIndex(&sec_it)), 16);
sec.segment[16] = '\0';
IterMustMove(&sec_it, 16);- In
MachO.c:286:
// proves the 8-byte prefix plus the 16-byte UUID payload fit.
IterMustMove(cmd, 8); // skip cmd + cmdsize prefix
MemCopy(ctx->out->uuid, IterDataAt(cmd, IterIndex(cmd)), 16);
ctx->out->has_uuid = true;
return true;- In
MachO.c:456:
return false;
}
MemCopy(BufData(©), data, data_size);
BufResize(©, (size)data_size);
return MachoOpenFromMemory(out, ©);- In
Elf.c:470:
return false;
}
MemCopy(BufData(©), data, data_size);
BufResize(©, (size)data_size);
// Hand `©` to the L-form -- it consumes the local and zeros
- In
Pdb.c:130:
if (chunk > remaining)
chunk = remaining;
MemCopy(dest, src + inoff, chunk);
dest += chunk;
offset += chunk;- In
Pdb.c:226:
want = num_dir_bytes - done;
}
MemCopy(self->stream_dir + done, src, want);
}
return true;- In
Pdb.c:336:
break;
}
MemCopy(self->info.guid, VecBegin(&buf) + 12, 16);
ok = true;
}- In
Pdb.c:739:
return false;
}
MemCopy(BufData(©), data, data_size);
BufResize(©, (size)data_size);
return PdbOpenFromMemory(out, ©);- In
Pe.c:386:
// 8-byte name: bytes, not a numeric, so copy + advance manually.
// IterRemainingLength >= 40 bound above proves 8 bytes are live.
MemCopy(s.name, IterDataAt(&c, IterIndex(&c)), 8);
s.name[8] = '\0';
IterMustMove(&c, 8);- In
Pe.c:477:
continue;
// Same proof: 16 bytes are live.
MemCopy(cv->guid, IterDataAt(&cv_cur, IterIndex(&cv_cur)), 16);
IterMustMove(&cv_cur, 16);
if (!BufReadU32LE(&cv_cur, &cv->age))- In
Pe.c:551:
return false;
}
MemCopy(BufData(©), data, data_size);
BufResize(©, (size)data_size);
return PeOpenFromMemory(out, ©);- In
Str.Ops.c:230:
StrIter *iter1 = VecPtrAt(&iters, 0);
char buffer1[10] = {0};
MemCopy(buffer1, iter1->data, StrIterLength(iter1));
result = result && (ZstrCompare(buffer1, "Hello") == 0);- In
Str.Ops.c:235:
StrIter *iter2 = VecPtrAt(&iters, 1);
char buffer2[10] = {0};
MemCopy(buffer2, iter2->data, StrIterLength(iter2));
result = result && (ZstrCompare(buffer2, "World") == 0);- In
Str.Ops.c:240:
StrIter *iter3 = VecPtrAt(&iters, 2);
char buffer3[10] = {0};
MemCopy(buffer3, iter3->data, StrIterLength(iter3));
result = result && (ZstrCompare(buffer3, "Test") == 0);
}- In
PdbCache.c:55:
return;
}
MemCopy(out, base, baselen);
out[baselen] = '/';
MemCopy(out + baselen + 1, name, namelen);- In
PdbCache.c:57:
MemCopy(out, base, baselen);
out[baselen] = '/';
MemCopy(out + baselen + 1, name, namelen);
out[baselen + 1 + namelen] = '\0';
}- In
PdbCache.c:142:
u8 *sec = &pe_blob[PE_SECTION_TBL_OFF];
const char nm[8] = {'.', 'd', 'e', 'b', 'u', 'g', 0, 0};
MemCopy(sec, nm, 8);
wr_u32(&sec[8], 0x200);
wr_u32(&sec[12], PE_DEBUG_DIR_RVA);- In
PdbCache.c:159:
cv[2] = 'D';
cv[3] = 'S';
MemCopy(&cv[4], kGuid, 16);
wr_u32(&cv[20], kAge);
u64 path_len = ZstrLen(pdb_path);- In
PdbCache.c:162:
wr_u32(&cv[20], kAge);
u64 path_len = ZstrLen(pdb_path);
MemCopy(&cv[24], pdb_path, path_len + 1);
}- In
PdbCache.c:205:
// Superblock
MemCopy(pdb_blob, kPdbMsfMagic, 32);
wr_u32(&pdb_blob[32], PDB_BLOCK_SIZE);
wr_u32(&pdb_blob[36], 1);- In
PdbCache.c:233:
wr_u32(&info[4], 0);
wr_u32(&info[8], kAge);
MemCopy(&info[12], kGuid, 16);
// DBI stream
- In
PdbCache.c:253:
wr_u32(&sym[8], func_rva - 0x1000);
wr_u16(&sym[12], 1);
MemCopy(&sym[14], func_name, name_len + 1);
// SectionHdr stream: one IMAGE_SECTION_HEADER with VA=0x1000.
- In
PdbCache.c:258:
u8 *sec = &pdb_blob[PDB_SECHDR_PAGE * PDB_BLOCK_SIZE];
const u8 sname[8] = {'.', 't', 'e', 'x', 't', 0, 0, 0};
MemCopy(sec, sname, 8);
wr_u32(&sec[8], 0x2000);
wr_u32(&sec[12], 0x1000); // Test very long strings
char long_number[100];
MemCopy(long_number, "12345678901234567890123456789012345678901234567890", 51);
Str long_str = StrInitFromZstr(long_number, &alloc);- In
MachO.c:67:
wr_u32(&seg[4], SEG64_HDR + SECT64_SIZE);
const char tname[16] = {'_', '_', 'T', 'E', 'X', 'T', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
MemCopy(&seg[8], tname, 16); // segname
wr_u64(&seg[24], 0x100000000ull); // vmaddr
wr_u64(&seg[32], 0x1000); // vmsize
- In
MachO.c:80:
u8 *sec = &blob[seg_off + SEG64_HDR];
const char sectname[16] = {'_', '_', 't', 'e', 'x', 't', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
MemCopy(&sec[0], sectname, 16); // sectname
MemCopy(&sec[16], tname, 16); // segname
wr_u64(&sec[32], 0x100000000ull); // addr
- In
MachO.c:81:
const char sectname[16] = {'_', '_', 't', 'e', 'x', 't', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
MemCopy(&sec[0], sectname, 16); // sectname
MemCopy(&sec[16], tname, 16); // segname
wr_u64(&sec[32], 0x100000000ull); // addr
wr_u64(&sec[40], 0x100); // size
- In
MachO.c:103:
wr_u32(&uc[0], 0x1B);
wr_u32(&uc[4], UUID_SIZE);
MemCopy(&uc[8], kUuid, 16);
// --- Symbol table (nlist_64 * 1) at offset 256 ------------------------
- In
MachO.c:117:
strs[0] = '\0';
const char nm[] = "my_function";
MemCopy(&strs[1], nm, sizeof(nm));
}- In
Vec.Complex.c:59:
if (!dst->name)
return false;
MemCopy(dst->name, src->name, name_len + 1);
} else {
dst->name = NULL;- In
Vec.Complex.c:73:
return false;
}
MemCopy(dst->values, src->values, src->num_values * sizeof(int));
} else {
dst->values = NULL; item.name = (char *)fixture_malloc(name_len + 1);
if (item.name) {
MemCopy(item.name, name, name_len);
item.name[name_len] = '\0';
} item.values = (int *)fixture_malloc(num_values * sizeof(int));
if (item.values) {
MemCopy(item.values, values, num_values * sizeof(int));
item.num_values = num_values;
}- In
Pdb.c:57:
// --- Superblock (page 0) -------------------------------------------------
MemCopy(blob, kMagic, 32);
wr_u32(&blob[32], BLOCK_SIZE);
wr_u32(&blob[36], 1); // free_block_map_block
- In
Pdb.c:89:
wr_u32(&info[4], 0xdeadbeef);
wr_u32(&info[8], 0x42);
MemCopy(&info[12], kGuid, 16);
}- In
Pdb.c:185:
// --- Superblock --------------------------------------------------------
MemCopy(fblob, kMagic, 32);
wr_u32(&fblob[32], F_BLOCK_SIZE);
wr_u32(&fblob[36], 1); // free_block_map_block
- In
Pdb.c:218:
wr_u32(&info[4], 0xfeedface);
wr_u32(&info[8], 1);
MemCopy(&info[12], kGuid, 16);
// --- DBI stream (#3) ---------------------------------------------------
- In
Pdb.c:263:
wr_u16(&sym[12], 1); // Segment (1-based)
const char name[] = "my_function";
MemCopy(&sym[14], name, sizeof(name)); // includes NUL
// --- SectionHdr stream (#5) --------------------------------------------
- In
Pdb.c:272:
// NumRelocs(2) + NumLineNum(2) + Characteristics(4)
const u8 sname[8] = {'.', 't', 'e', 'x', 't', 0, 0, 0};
MemCopy(sec, sname, 8);
wr_u32(&sec[8], 0x2000); // VirtualSize
wr_u32(&sec[12], 0x1000); // VirtualAddress
- In
MachoCache.c:111:
wr_u32(&seg[4], SEG64_HDR + SECT64_SIZE);
const char tname[16] = {'_', '_', 'T', 'E', 'X', 'T', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
MemCopy(&seg[8], tname, 16);
wr_u64(&seg[24], 0x100000000ull);
wr_u64(&seg[32], 0x1000);- In
MachoCache.c:122:
u8 *sec = &out[seg_off + SEG64_HDR];
const char sectname[16] = {'_', '_', 't', 'e', 'x', 't', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
MemCopy(&sec[0], sectname, 16);
MemCopy(&sec[16], tname, 16);
wr_u64(&sec[32], 0x100000000ull);- In
MachoCache.c:123:
const char sectname[16] = {'_', '_', 't', 'e', 'x', 't', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
MemCopy(&sec[0], sectname, 16);
MemCopy(&sec[16], tname, 16);
wr_u64(&sec[32], 0x100000000ull);
wr_u64(&sec[40], 0x1000);- In
MachoCache.c:141:
wr_u32(&uc[0], 0x1B);
wr_u32(&uc[4], UUID_SIZE);
MemCopy(&uc[8], uuid, 16);
// Symbols + strings.
- In
MachoCache.c:157:
while (s[nlen])
++nlen;
MemCopy(&out[str_off + cur_strx], s, nlen);
out[str_off + cur_strx + nlen] = '\0';
cur_strx += nlen + 1;- In
MachoCache.c:256:
u8 bad_uuid[16];
MemCopy(bad_uuid, kUuid, 16);
bad_uuid[0] ^= 0xff;- In
Pe.c:120:
u8 *sec = &blob[SECTION_TBL_OFF];
const char nm[8] = {'.', 'd', 'e', 'b', 'u', 'g', 0, 0};
MemCopy(sec, nm, 8);
wr_u32(&sec[8], SECTION_VSIZE);
wr_u32(&sec[12], SECTION_VA);- In
Pe.c:148:
cv[2] = 'D';
cv[3] = 'S';
MemCopy(&cv[4], kGuid, 16);
wr_u32(&cv[20], 0x0000002a); // Age
// PDB path, NUL-terminated.
- In
Pe.c:152:
// PDB path, NUL-terminated.
u64 path_len = ZstrLen(kPdbPath);
MemCopy(&cv[24], kPdbPath, path_len + 1);
}
Last updated on