Skip to content

Commit

Permalink
Convert some H5MM calls to standard C equivalents (#2382)
Browse files Browse the repository at this point in the history
* H5MM_calloc and malloc are now mapped to stdlib C calls
* H5MM_memcpy now maps directly to memcpy in release builds
* H5MM_memcpy is still implemented as a separate function that
   checks for buffer overlap when H5MM_DEBUG is defined
   (default w/ debug builds)
* Switches many library memcpy calls to use H5MM_memcpy
* Fixes a possible zero allocation in H5Olayout.c
  • Loading branch information
derobins authored Aug 25, 2023
1 parent 3dd60d9 commit 627f7c5
Show file tree
Hide file tree
Showing 23 changed files with 142 additions and 174 deletions.
1 change: 1 addition & 0 deletions config/cmake/HDFCompilerFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ if (HDF5_ENABLE_DEBUG_APIS)
H5F_DEBUG
H5HL_DEBUG
H5I_DEBUG
H5MM_DEBUG
H5O_DEBUG
H5S_DEBUG
H5T_DEBUG
Expand Down
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2492,8 +2492,8 @@ AC_SUBST([INTERNAL_DEBUG_OUTPUT])
## too specialized or have huge performance hits. These
## are not listed in the "all" packages list.
##
## all_packages="AC,B,B2,D,F,FA,FL,FS,HL,I,O,S,T,Z"
all_packages="AC,B2,CX,D,F,HL,I,O,S,T,Z"
## all_packages="AC,B,B2,D,F,FA,FL,FS,HL,I,MM,O,S,T,Z"
all_packages="AC,B2,CX,D,F,HL,I,MM,O,S,T,Z"

case "X-$INTERNAL_DEBUG_OUTPUT" in
X-yes|X-all)
Expand Down
2 changes: 1 addition & 1 deletion src/H5CS.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ H5CS_copy_stack(void)

/* Copy pointers on old stack to new one */
/* (Strings don't need to be duplicated, they are statically allocated) */
memcpy(new_stack->rec, old_stack->rec, sizeof(char *) * old_stack->nused);
H5MM_memcpy(new_stack->rec, old_stack->rec, sizeof(char *) * old_stack->nused);
new_stack->nused = new_stack->nalloc = old_stack->nused;

/* Set the return value */
Expand Down
8 changes: 4 additions & 4 deletions src/H5Dchunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -2790,7 +2790,7 @@ H5D__chunk_read(H5D_io_info_t *io_info, H5D_dset_io_info_t *dset_info)

/* Set up contiguous I/O info object */
H5MM_memcpy(&ctg_io_info, io_info, sizeof(ctg_io_info));
memcpy(&ctg_dset_info, dset_info, sizeof(ctg_dset_info));
H5MM_memcpy(&ctg_dset_info, dset_info, sizeof(ctg_dset_info));
ctg_dset_info.store = &ctg_store;
ctg_dset_info.layout_ops = *H5D_LOPS_CONTIG;
ctg_io_info.dsets_info = &ctg_dset_info;
Expand All @@ -2802,7 +2802,7 @@ H5D__chunk_read(H5D_io_info_t *io_info, H5D_dset_io_info_t *dset_info)

/* Set up compact I/O info object */
H5MM_memcpy(&cpt_io_info, io_info, sizeof(cpt_io_info));
memcpy(&cpt_dset_info, dset_info, sizeof(cpt_dset_info));
H5MM_memcpy(&cpt_dset_info, dset_info, sizeof(cpt_dset_info));
cpt_dset_info.store = &cpt_store;
cpt_dset_info.layout_ops = *H5D_LOPS_COMPACT;
cpt_io_info.dsets_info = &cpt_dset_info;
Expand Down Expand Up @@ -2950,7 +2950,7 @@ H5D__chunk_write(H5D_io_info_t *io_info, H5D_dset_io_info_t *dset_info)

/* Set up contiguous I/O info object */
H5MM_memcpy(&ctg_io_info, io_info, sizeof(ctg_io_info));
memcpy(&ctg_dset_info, dset_info, sizeof(ctg_dset_info));
H5MM_memcpy(&ctg_dset_info, dset_info, sizeof(ctg_dset_info));
ctg_dset_info.store = &ctg_store;
ctg_dset_info.layout_ops = *H5D_LOPS_CONTIG;
ctg_io_info.dsets_info = &ctg_dset_info;
Expand All @@ -2962,7 +2962,7 @@ H5D__chunk_write(H5D_io_info_t *io_info, H5D_dset_io_info_t *dset_info)

/* Set up compact I/O info object */
H5MM_memcpy(&cpt_io_info, io_info, sizeof(cpt_io_info));
memcpy(&cpt_dset_info, dset_info, sizeof(cpt_dset_info));
H5MM_memcpy(&cpt_dset_info, dset_info, sizeof(cpt_dset_info));
cpt_dset_info.store = &cpt_store;
cpt_dset_info.layout_ops = *H5D_LOPS_COMPACT;
cpt_io_info.dsets_info = &cpt_dset_info;
Expand Down
4 changes: 2 additions & 2 deletions src/H5Dmpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -3772,7 +3772,7 @@ H5D__mpio_share_chunk_modification_data(H5D_filtered_collective_io_info_t *chunk
mod_data_p = msg_send_bufs[num_send_requests];

/* Store the chunk's index into the buffer */
memcpy(mod_data_p, &chunk_entry->index_info.chunk_idx, sizeof(hsize_t));
H5MM_memcpy(mod_data_p, &chunk_entry->index_info.chunk_idx, sizeof(hsize_t));
mod_data_p += sizeof(hsize_t);

/* Serialize the chunk's file dataspace into the buffer */
Expand Down Expand Up @@ -4491,7 +4491,7 @@ H5D__mpio_collective_filtered_chunk_update(H5D_filtered_collective_io_info_t *ch

if (msg_ptr) {
/* Retrieve the chunk's index value */
memcpy(&chunk_idx, msg_ptr, sizeof(hsize_t));
H5MM_memcpy(&chunk_idx, msg_ptr, sizeof(hsize_t));
msg_ptr += sizeof(hsize_t);

/* Find the chunk entry according to its chunk index */
Expand Down
4 changes: 2 additions & 2 deletions src/H5Dscatgath.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ H5D__scatter_file(const H5D_io_info_t *_io_info, const H5D_dset_io_info_t *_dset

/* Set up temporary I/O info object */
H5MM_memcpy(&tmp_io_info, _io_info, sizeof(*_io_info));
memcpy(&tmp_dset_info, _dset_info, sizeof(*_dset_info));
H5MM_memcpy(&tmp_dset_info, _dset_info, sizeof(*_dset_info));
tmp_io_info.op_type = H5D_IO_OP_WRITE;
tmp_dset_info.buf.cvp = _buf;
tmp_io_info.dsets_info = &tmp_dset_info;
Expand Down Expand Up @@ -219,7 +219,7 @@ H5D__gather_file(const H5D_io_info_t *_io_info, const H5D_dset_io_info_t *_dset_

/* Set up temporary I/O info object */
H5MM_memcpy(&tmp_io_info, _io_info, sizeof(*_io_info));
memcpy(&tmp_dset_info, _dset_info, sizeof(*_dset_info));
H5MM_memcpy(&tmp_dset_info, _dset_info, sizeof(*_dset_info));
tmp_io_info.op_type = H5D_IO_OP_READ;
tmp_dset_info.buf.vp = _buf;
tmp_io_info.dsets_info = &tmp_dset_info;
Expand Down
10 changes: 5 additions & 5 deletions src/H5FDonion.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ H5Pget_fapl_onion(hid_t fapl_id, H5FD_onion_fapl_info_t *fa_out)
if (NULL == (info_ptr = (const H5FD_onion_fapl_info_t *)H5P_peek_driver_info(plist)))
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "bad VFL driver info");

memcpy(fa_out, info_ptr, sizeof(H5FD_onion_fapl_info_t));
H5MM_memcpy(fa_out, info_ptr, sizeof(H5FD_onion_fapl_info_t));

done:
FUNC_LEAVE_API(ret_value)
Expand Down Expand Up @@ -514,7 +514,7 @@ H5FD__onion_commit_new_revision_record(H5FD_onion_t *file)

if (NULL == (new_list = H5MM_calloc((history->n_revisions + 1) * sizeof(H5FD_onion_record_loc_t))))
HGOTO_ERROR(H5E_VFL, H5E_CANTALLOC, FAIL, "unable to resize record pointer list");
memcpy(new_list, history->record_locs, sizeof(H5FD_onion_record_loc_t) * history->n_revisions);
H5MM_memcpy(new_list, history->record_locs, sizeof(H5FD_onion_record_loc_t) * history->n_revisions);
H5MM_xfree(history->record_locs);
history->record_locs = new_list;
new_list = NULL;
Expand Down Expand Up @@ -980,7 +980,7 @@ H5FD__onion_open(const char *filename, unsigned flags, hid_t fapl_id, haddr_t ma

/* Initialize file structure fields */

memcpy(&(file->fa), fa, sizeof(H5FD_onion_fapl_info_t));
H5MM_memcpy(&(file->fa), fa, sizeof(H5FD_onion_fapl_info_t));

file->header.version = H5FD_ONION_HEADER_VERSION_CURR;
file->header.page_size = file->fa.page_size; /* guarded on FAPL-set */
Expand Down Expand Up @@ -1523,7 +1523,7 @@ H5FD__onion_write(H5FD_t *_file, H5FD_mem_t type, hid_t H5_ATTR_UNUSED dxpl_id,
if (H5FD_read(file->onion_file, H5FD_MEM_DRAW, entry_out->phys_addr, page_size, page_buf) < 0)
HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "can't get working file data");
/* Overlay delta from input buffer onto page buffer. */
memcpy(page_buf + page_gap_head, buf, page_n_used);
H5MM_memcpy(page_buf + page_gap_head, buf, page_n_used);
write_buf = page_buf;
} /* end if partial page */

Expand Down Expand Up @@ -1571,7 +1571,7 @@ H5FD__onion_write(H5FD_t *_file, H5FD_mem_t type, hid_t H5_ATTR_UNUSED dxpl_id,

/* Copy input buffer to temporary page buffer */
assert((page_size - page_gap_head) >= page_n_used);
memcpy(page_buf + page_gap_head, buf, page_n_used);
H5MM_memcpy(page_buf + page_gap_head, buf, page_n_used);
write_buf = page_buf;

} /* end if data range does not span entire page */
Expand Down
16 changes: 8 additions & 8 deletions src/H5FDonion_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,34 +142,34 @@ H5FD__onion_header_decode(unsigned char *buf, H5FD_onion_header_t *header)

ptr = buf + 5;
ui32 = 0;
memcpy(&ui32, ptr, 3);
H5MM_memcpy(&ui32, ptr, 3);
ui8p = (uint8_t *)&ui32;
UINT32DECODE(ui8p, header->flags);
ptr += 3;

memcpy(&ui32, ptr, 4);
H5MM_memcpy(&ui32, ptr, 4);
ui8p = (uint8_t *)&ui32;
UINT32DECODE(ui8p, header->page_size);
ptr += 4;

memcpy(&ui64, ptr, 8);
H5MM_memcpy(&ui64, ptr, 8);
ui8p = (uint8_t *)&ui64;
UINT32DECODE(ui8p, header->origin_eof);
ptr += 8;

memcpy(&ui64, ptr, 8);
H5MM_memcpy(&ui64, ptr, 8);
ui8p = (uint8_t *)&ui64;
UINT32DECODE(ui8p, header->history_addr);
ptr += 8;

memcpy(&ui64, ptr, 8);
H5MM_memcpy(&ui64, ptr, 8);
ui8p = (uint8_t *)&ui64;
UINT32DECODE(ui8p, header->history_size);
ptr += 8;

sum = H5_checksum_fletcher32(buf, (size_t)(ptr - buf));

memcpy(&ui32, ptr, 4);
H5MM_memcpy(&ui32, ptr, 4);
ui8p = (uint8_t *)&ui32;
UINT32DECODE(ui8p, header->checksum);
ptr += 4;
Expand Down Expand Up @@ -214,9 +214,9 @@ H5FD__onion_header_encode(H5FD_onion_header_t *header, unsigned char *buf, uint3
assert(H5FD_ONION_HEADER_VERSION_CURR == header->version);
assert(0 == (header->flags & 0xFF000000)); /* max three bits long */

memcpy(ptr, H5FD_ONION_HEADER_SIGNATURE, 4);
H5MM_memcpy(ptr, H5FD_ONION_HEADER_SIGNATURE, 4);
ptr += 4;
memcpy(ptr, (unsigned char *)&header->version, 1);
H5MM_memcpy(ptr, (unsigned char *)&header->version, 1);
ptr += 1;
UINT32ENCODE(ptr, header->flags);
ptr -= 1; /* truncate to three bytes */
Expand Down
12 changes: 6 additions & 6 deletions src/H5FDonion_history.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ H5FD__onion_history_decode(unsigned char *buf, H5FD_onion_history_t *history)

ptr = buf + 8;

memcpy(&ui64, ptr, 8);
H5MM_memcpy(&ui64, ptr, 8);
ui8p = (uint8_t *)&ui64;
UINT64DECODE(ui8p, n_revisions);
ptr += 8;
Expand All @@ -207,19 +207,19 @@ H5FD__onion_history_decode(unsigned char *buf, H5FD_onion_history_t *history)
uint64_t record_size;
uint64_t phys_addr;

memcpy(&ui64, ptr, 8);
H5MM_memcpy(&ui64, ptr, 8);
ui8p = (uint8_t *)&ui64;
UINT64DECODE(ui8p, phys_addr);
H5_CHECKED_ASSIGN(rloc->phys_addr, haddr_t, phys_addr, uint64_t);
ptr += 8;

memcpy(&ui64, ptr, 8);
H5MM_memcpy(&ui64, ptr, 8);
ui8p = (uint8_t *)&ui64;
UINT64DECODE(ui8p, record_size);
H5_CHECKED_ASSIGN(rloc->record_size, hsize_t, record_size, uint64_t);
ptr += 8;

memcpy(&ui32, ptr, 4);
H5MM_memcpy(&ui32, ptr, 4);
ui8p = (uint8_t *)&ui32;
UINT32DECODE(ui8p, rloc->checksum);
ptr += 4;
Expand All @@ -228,7 +228,7 @@ H5FD__onion_history_decode(unsigned char *buf, H5FD_onion_history_t *history)

sum = H5_checksum_fletcher32(buf, (size_t)(ptr - buf));

memcpy(&ui32, ptr, 4);
H5MM_memcpy(&ui32, ptr, 4);
ui8p = (uint8_t *)&ui32;
UINT32DECODE(ui8p, history->checksum);
ptr += 4;
Expand Down Expand Up @@ -275,7 +275,7 @@ H5FD__onion_history_encode(H5FD_onion_history_t *history, unsigned char *buf, ui
assert(buf != NULL);
assert(checksum != NULL);

memcpy(ptr, H5FD_ONION_HISTORY_SIGNATURE, 4);
H5MM_memcpy(ptr, H5FD_ONION_HISTORY_SIGNATURE, 4);
ptr += 4;
UINT32ENCODE(ptr, vers_u32);
UINT64ENCODE(ptr, history->n_revisions);
Expand Down
Loading

0 comments on commit 627f7c5

Please sign in to comment.