mem_malloc type triviality checks (8/8)

(Manual modification.) Change mem_malloc callsites with
non-fundamental types to mem_rmalloc.
This commit is contained in:
Jan Engelhardt 2024-09-08 13:29:41 +02:00
parent a23c6a42a3
commit 3c9234c4c0
18 changed files with 53 additions and 62 deletions

View File

@ -256,7 +256,7 @@ void ReadAmbientData() {
asp->num_sounds = cf_ReadInt(ifile);
if (asp->num_sounds > 0)
asp->sounds = (ase *)mem_malloc(sizeof(*asp->sounds) * asp->num_sounds);
asp->sounds = mem_rmalloc<ase>(asp->num_sounds);
else
asp->sounds = NULL;

View File

@ -785,7 +785,7 @@ int DemoReadHeader() {
if (gs_Xlates)
mem_free(gs_Xlates);
gs_Xlates = (struct gs_tables *)mem_malloc(sizeof(gs_tables));
gs_Xlates = mem_rmalloc<gs_tables>();
try {
LGSXlateTables(Demo_cfp);

View File

@ -646,7 +646,7 @@ doorway *DoorwayAdd(room *rp, int doornum) {
rp->flags |= RF_DOOR;
doorway *dp = rp->doorway_data = (doorway *)mem_malloc(sizeof(*rp->doorway_data));
auto dp = rp->doorway_data = mem_rmalloc<doorway>();
// Initialize
dp->doornum = doornum;

View File

@ -838,7 +838,7 @@ int LGSObjects(CFILE *fp, int version) {
int i, j, highest_index;
int max_terr;
matrix *objmat = (matrix *)mem_malloc(sizeof(*objmat) * MAX_OBJECTS);
auto objmat = mem_rmalloc<matrix>(MAX_OBJECTS);
Osiris_DisableCreateEvents();
// we must reset some data before continuing.
@ -992,7 +992,7 @@ int LGSObjects(CFILE *fp, int version) {
gs_ReadByte(fp, has_lightinfo);
if (has_lightinfo) {
if (!op->lighting_info) {
op->lighting_info = (light_info *)mem_malloc(sizeof(*op->lighting_info));
op->lighting_info = mem_rmalloc<light_info>();
}
cf_ReadBytes((uint8_t *)op->lighting_info, sizeof(*op->lighting_info), fp);
}
@ -1443,7 +1443,6 @@ done:;
// loads ai
int LGSObjAI(CFILE *fp, ai_frame **pai) {
ai_frame *ai;
int16_t size;
int8_t read_ai;
@ -1457,9 +1456,7 @@ int LGSObjAI(CFILE *fp, ai_frame **pai) {
if (size != sizeof(ai_frame))
return LGS_OUTDATEDVER;
*pai = (ai_frame *)mem_malloc(size);
ai = *pai;
auto ai = *pai = mem_rmalloc<ai_frame>();
cf_ReadBytes((uint8_t *)ai, size, fp);
return LGS_OK;
@ -1478,9 +1475,7 @@ int LGSObjEffects(CFILE *fp, object *op) {
if (size != sizeof(effect_info_s))
return LGS_OUTDATEDVER;
op->effect_info = (effect_info_s *)mem_malloc(size);
effect_info_s *ei = op->effect_info;
auto ei = op->effect_info = mem_rmalloc<effect_info_s>();
cf_ReadBytes((uint8_t *)ei, size, fp);
}

View File

@ -1203,7 +1203,7 @@ void newuiSheet::Create(UIWindow *menu, const char *title, int n_items, int sx,
m_gadgetlimit = n_items;
m_ngadgets = 0;
if (n_items) {
m_gadgetlist = (newuiSheet::t_gadget_desc *)mem_malloc(n_items * sizeof(newuiSheet::t_gadget_desc));
m_gadgetlist = mem_rmalloc<t_gadget_desc>(n_items);
} else {
m_gadgetlist = NULL;
}

View File

@ -630,7 +630,7 @@ void ObjSetLocalLighting(object *objp) {
(objp->type == OBJ_BUILDING));
// allocate a light info for this object
objp->lighting_info = (light_info *)mem_malloc(sizeof(*objp->lighting_info));
objp->lighting_info = mem_rmalloc<light_info>();
// copy the lighting info from the type for this object
*objp->lighting_info = Object_info[objp->id].lighting_info;

View File

@ -899,7 +899,7 @@ void pilot::add_mission_data(tMissionData *mdata) {
LOG_DEBUG.printf("Adding new mission data for (%s)", mdata->mission_name);
tMissionData *new_data = (tMissionData *)mem_malloc((num_missions_flown + 1) * sizeof(tMissionData));
auto new_data = mem_rmalloc<tMissionData>(num_missions_flown + 1);
if (!new_data) {
LOG_WARNING << "Out of memory";
return;

View File

@ -567,7 +567,7 @@ void InitRoom(room *rp, int nverts, int nfaces, int nportals) {
ASSERT(rp->verts != NULL);
if (Katmai) {
rp->verts4 = (vector4 *)mem_malloc(nverts * sizeof(*rp->verts4));
rp->verts4 = mem_rmalloc<vector4>(nverts);
ASSERT(rp->verts4 != NULL);
}
@ -667,7 +667,7 @@ void FreeRoom(room *rp) {
RoomMemFree(rp->portals);
RoomMemFree(rp->verts);
if (Katmai)
if (Katmai )
mem_free(rp->verts4);
if (rp->num_bbf_regions) {

View File

@ -957,7 +957,7 @@ void InitTerrain(void) {
Terrain_rotate_list = (uint16_t *)mem_malloc(TERRAIN_WIDTH * TERRAIN_DEPTH * sizeof(uint16_t));
ASSERT(Terrain_rotate_list);
World_point_buffer = (g3Point *)mem_malloc(TERRAIN_WIDTH * TERRAIN_DEPTH * sizeof(g3Point));
World_point_buffer = mem_rmalloc<g3Point>(TERRAIN_WIDTH * TERRAIN_DEPTH);
ASSERT(World_point_buffer);
// Allocate space for lod delta tree and unique texture IDs
@ -975,7 +975,7 @@ void InitTerrain(void) {
w = TERRAIN_WIDTH >> ((MAX_TERRAIN_LOD - 1) - i);
h = TERRAIN_DEPTH >> ((MAX_TERRAIN_LOD - 1) - i);
TerrainNormals[i] = (terrain_normals *)mem_malloc(w * h * sizeof(terrain_normals));
TerrainNormals[i] = mem_rmalloc<terrain_normals>(w * h);
memset(TerrainNormals[i], 0, w * h * sizeof(terrain_normals));
}

View File

@ -282,7 +282,7 @@ CFILE *cf_OpenFileInLibrary(const std::filesystem::path &filename, int libhandle
return nullptr;
}
}
CFILE *cfile = (CFILE *)mem_malloc(sizeof(*cfile));
auto cfile = mem_rmalloc<CFILE>();
if (!cfile)
Error("Out of memory in cf_OpenFileInLibrary()");
cfile->name = lib->entries[i]->name;
@ -335,7 +335,7 @@ CFILE *open_file_in_lib(const char *filename) {
return nullptr;
}
}
cfile = (CFILE *)mem_malloc(sizeof(*cfile));
cfile = mem_rmalloc<CFILE>();
if (!cfile)
Error("Out of memory in open_file_in_lib()");
cfile->name = lib->entries[i]->name;
@ -451,7 +451,7 @@ CFILE *open_file_in_directory(const std::filesystem::path &filename, const char
}
// found the file, open it
cfile = (CFILE *)mem_malloc(sizeof(*cfile));
cfile = mem_rmalloc<CFILE>();
if (!cfile)
Error("Out of memory in open_file_in_directory()");
cfile->name = mem_rmalloc<char>((strlen(using_filename.u8string().c_str()) + 1));

View File

@ -217,7 +217,7 @@ void CAmbientSoundPattern::OnASPNewElement() {
if (new_element.handle != -1) {
ase *new_sounds = (ase *)mem_malloc(sizeof(*new_sounds) * (asp->num_sounds + 1));
auto new_sounds = mem_rmalloc<ase>(asp->num_sounds + 1);
for (int s = 0; s < asp->num_sounds; s++)
new_sounds[s] = asp->sounds[s];
@ -252,7 +252,7 @@ void CAmbientSoundPattern::OnASPDeleteElement() {
if (m_current_element > -1) {
if (OutrageMessageBox(MBOX_YESNO, "Do you really want to delete sound '%s' from pattern '%s'?",
Sounds[asp->sounds[m_current_element].handle].name, asp->name) == IDYES) {
new_sounds = (ase *)mem_malloc(sizeof(*new_sounds) * (asp->num_sounds - 1));
new_sounds = mem_rmalloc<ase>(asp->num_sounds - 1);
for (int s = 0; s < m_current_element; s++)
new_sounds[s] = asp->sounds[s];

View File

@ -1343,7 +1343,7 @@ void ReInitRoomFace(face *fp, int nverts) {
fp->face_verts = (int16_t *)mem_malloc(nverts * sizeof(*fp->face_verts));
ASSERT(fp->face_verts != NULL);
fp->face_uvls = (roomUVL *)mem_malloc(nverts * sizeof(*fp->face_uvls));
fp->face_uvls = mem_rmalloc<roomUVL>(nverts);
ASSERT(fp->face_uvls != NULL);
}
@ -1395,7 +1395,7 @@ int RoomAddVertices(room *rp, int num_new_verts) {
if (num_new_verts == 0)
return 0;
vector *newverts = (vector *)mem_malloc((rp->num_verts + num_new_verts) * sizeof(*newverts));
auto newverts = mem_rmalloc<vector>(rp->num_verts + num_new_verts);
ASSERT(newverts != NULL);
@ -1422,7 +1422,7 @@ int RoomAddFaces(room *rp, int num_new_faces) {
if (num_new_faces == 0)
return 0;
face *newfaces = (face *)mem_malloc((rp->num_faces + num_new_faces) * sizeof(*newfaces));
auto newfaces = mem_rmalloc<face>(rp->num_faces + num_new_faces);
ASSERT(newfaces != NULL);
@ -1635,7 +1635,7 @@ void DeleteRoomVert(room *rp, int vertnum) {
fp->face_verts[v]--;
// malloc new list
newverts = (vector *)mem_malloc(sizeof(*newverts) * (rp->num_verts - 1));
newverts = mem_rmalloc<vector>(rp->num_verts - 1);
ASSERT(newverts != NULL);
// Copy verts to new list
@ -1717,7 +1717,7 @@ void DeleteRoomFace(room *rp, int facenum, bool delete_unused_verts) {
}
// Allocate new face list
newfaces = (face *)mem_malloc(sizeof(*newfaces) * (rp->num_faces - 1));
newfaces = mem_rmalloc<face>(rp->num_faces - 1);
ASSERT(newfaces != NULL);
// Copy faces over
@ -1810,7 +1810,7 @@ void DeleteRoomPortal(room *rp, int portalnum) {
if (rp->num_portals == 1)
newportals = NULL;
else {
newportals = (portal *)mem_malloc(sizeof(*newportals) * (rp->num_portals - 1));
newportals = mem_rmalloc<portal>(rp->num_portals - 1);
ASSERT(newportals != NULL);
}
@ -1956,7 +1956,7 @@ void CopyRoom(room *destp, room *srcp) {
// Copy doorway info
if (srcp->doorway_data) {
destp->doorway_data = (doorway *)mem_malloc(sizeof(*destp->doorway_data));
destp->doorway_data = mem_rmalloc<doorway>();
*destp->doorway_data = *srcp->doorway_data;
}
@ -1973,9 +1973,7 @@ void CopyRoom(room *destp, room *srcp) {
// Adds a new portal for this room. Returns the portal number.
// Initializes the flags
int AddPortal(room *rp) {
portal *newlist;
newlist = (portal *)mem_malloc(sizeof(*newlist) * (rp->num_portals + 1));
auto newlist = mem_rmalloc<portal>(rp->num_portals + 1);
// Copy from old list to new list, and free old list
if (rp->num_portals) {

View File

@ -236,16 +236,15 @@ void FreeGroup(group *g) {
// attachroom, attachface - where group attaches when pasted
// Returns: pointer to group
group *CopyGroup(int nrooms, int *roomnums, int attachroom, int attachface) {
group *g;
int r;
int room_xlate[MAX_ROOMS];
int n_objects = 0, n_triggers = 0, n_doors = 0;
// Allocate group & room arrays
g = (group *)mem_malloc(sizeof(*g));
auto g = mem_rmalloc<group>();
ASSERT(g != NULL);
g->nrooms = nrooms;
g->rooms = (room *)mem_malloc(sizeof(*g->rooms) * nrooms);
g->rooms = mem_rmalloc<room>(nrooms);
ASSERT(g->rooms != NULL);
// Initialize xlate list
@ -296,7 +295,7 @@ group *CopyGroup(int nrooms, int *roomnums, int attachroom, int attachface) {
int objnum = 0;
// Get memory for objects
g->objects = (object *)mem_malloc(sizeof(*g->objects) * n_objects);
g->objects = mem_rmalloc<object>(n_objects);
// Go through list of rooms & copy the objects
for (r = 0; r < nrooms; r++) {
@ -344,7 +343,7 @@ group *CopyGroup(int nrooms, int *roomnums, int attachroom, int attachface) {
int trignum = 0;
// Get memory for triggers
g->triggers = (trigger *)mem_malloc(sizeof(*g->triggers) * n_triggers);
g->triggers = mem_rmalloc<trigger>(n_triggers);
for (int t = 0; t < Num_triggers; t++)
if (room_xlate[Triggers[t].roomnum] != -1) {
@ -754,7 +753,6 @@ group *LoadGroup(char *filename) {
CFILE *ifile;
char tag[4];
int version, level_version;
group *g;
int i;
ifile = cfopen(filename, "rb");
@ -794,7 +792,7 @@ group *LoadGroup(char *filename) {
ReadTextureList(ifile);
// Allocate group
g = (group *)mem_malloc(sizeof(*g));
auto g = mem_rmalloc<group>();
// Read group info
g->nrooms = cf_ReadInt(ifile);
@ -810,7 +808,7 @@ group *LoadGroup(char *filename) {
g->nobjects = g->ndoors = g->ntriggers = 0;
// Allocate room and vertex arrays
g->rooms = (room *)mem_malloc(sizeof(*g->rooms) * g->nrooms);
g->rooms = mem_rmalloc<room>(g->nrooms);
// Read rooms
for (i = 0; i < g->nrooms; i++) {
@ -825,11 +823,11 @@ group *LoadGroup(char *filename) {
// Allocate objects array
if (g->nobjects)
g->objects = (object *)mem_malloc(sizeof(*g->objects) * g->nobjects);
g->objects = mem_rmalloc<object>(g->nobjects);
// Allocate triggers array
if (g->ntriggers)
g->triggers = (trigger *)mem_malloc(sizeof(*g->triggers) * g->ntriggers);
g->triggers = mem_rmalloc<trigger>(g->ntriggers);
// Read objects
for (i = 0; i < g->nobjects; i++)

View File

@ -903,7 +903,7 @@ void DoRadiosityForRooms() {
Rooms[roomnum].volume_lights = (uint8_t *)mem_malloc(vw * vh * vd);
ASSERT(Rooms[roomnum].volume_lights);
Volume_elements[roomnum] = (volume_element *)mem_malloc((vw * vh * vd) * sizeof(volume_element));
Volume_elements[roomnum] = mem_rmalloc<volume_element>(vw * vh * vd);
ASSERT(Volume_elements[roomnum]);
// Now go through and find all the valid spectra points
@ -977,8 +977,8 @@ void DoRadiosityForRooms() {
if (Light_surfaces[surface_index].xresolution * Light_surfaces[surface_index].yresolution) {
Light_surfaces[surface_index].elements =
(rad_element *)mem_malloc(Light_surfaces[surface_index].xresolution *
Light_surfaces[surface_index].yresolution * sizeof(rad_element));
mem_rmalloc<rad_element>(Light_surfaces[surface_index].xresolution *
Light_surfaces[surface_index].yresolution);
ASSERT(Light_surfaces[surface_index].elements != NULL);
} else {
Light_surfaces[surface_index].elements = NULL;
@ -1223,8 +1223,8 @@ void DoRadiosityForCurrentRoom(room *rp) {
}
if (Light_surfaces[surface_index].xresolution * Light_surfaces[surface_index].yresolution) {
Light_surfaces[surface_index].elements = (rad_element *)mem_malloc(
Light_surfaces[surface_index].xresolution * Light_surfaces[surface_index].yresolution * sizeof(rad_element));
Light_surfaces[surface_index].elements = mem_rmalloc<rad_element>(
Light_surfaces[surface_index].xresolution * Light_surfaces[surface_index].yresolution);
ASSERT(Light_surfaces[surface_index].elements != NULL);
} else {
Light_surfaces[surface_index].elements = NULL;
@ -1873,8 +1873,8 @@ void DoRadiosityForTerrain() {
ClearAllObjectLightmaps(1);
// Allocate memory
terrain_sums[0] = (spectra *)mem_malloc(TERRAIN_WIDTH * TERRAIN_DEPTH * sizeof(spectra));
terrain_sums[1] = (spectra *)mem_malloc(TERRAIN_WIDTH * TERRAIN_DEPTH * sizeof(spectra));
terrain_sums[0] = mem_rmalloc<spectra>(TERRAIN_WIDTH * TERRAIN_DEPTH);
terrain_sums[1] = mem_rmalloc<spectra>(TERRAIN_WIDTH * TERRAIN_DEPTH);
ASSERT(terrain_sums[0] && terrain_sums[1]);
Light_surfaces = mem_rmalloc<rad_surface>(total_surfaces);
@ -2044,8 +2044,8 @@ void DoRadiosityForTerrain() {
Light_surfaces[surf_index].verts = mem_rmalloc<vector>(Rooms[i].faces[t].num_verts);
ASSERT(Light_surfaces[surf_index].verts != NULL);
Light_surfaces[surf_index].elements = (rad_element *)mem_malloc(
Light_surfaces[surf_index].xresolution * Light_surfaces[surf_index].yresolution * sizeof(rad_element));
Light_surfaces[surf_index].elements = mem_rmalloc<rad_element>(
Light_surfaces[surf_index].xresolution * Light_surfaces[surf_index].yresolution);
ASSERT(Light_surfaces[surf_index].elements != NULL);
if (Rooms[i].faces[t].portal_num != -1 &&

View File

@ -321,8 +321,8 @@ int ComputeSurfacesForObjects(int surface_index, int terrain) {
if (Light_surfaces[surface_index].xresolution * Light_surfaces[surface_index].yresolution > 0) {
Light_surfaces[surface_index].elements =
(rad_element *)mem_malloc(Light_surfaces[surface_index].xresolution *
Light_surfaces[surface_index].yresolution * sizeof(rad_element));
mem_rmalloc<rad_element>(Light_surfaces[surface_index].xresolution *
Light_surfaces[surface_index].yresolution);
ASSERT(Light_surfaces[surface_index].elements != NULL);
} else
Light_surfaces[surface_index].elements = NULL;
@ -407,8 +407,8 @@ int ComputeSurfacesForObjectsForSingleRoom(int surface_index, int roomnum) {
if (Light_surfaces[surface_index].xresolution * Light_surfaces[surface_index].yresolution > 0) {
Light_surfaces[surface_index].elements =
(rad_element *)mem_malloc(Light_surfaces[surface_index].xresolution *
Light_surfaces[surface_index].yresolution * sizeof(rad_element));
mem_rmalloc<rad_element>(Light_surfaces[surface_index].xresolution *
Light_surfaces[surface_index].yresolution);
ASSERT(Light_surfaces[surface_index].elements != NULL);
} else
Light_surfaces[surface_index].elements = NULL;

View File

@ -1466,7 +1466,7 @@ void mng_TransferPages() {
Int3();
return;
}
mngs_track_lock *local_tracklocks = (mngs_track_lock *)mem_malloc(sizeof(*local_tracklocks) * 5000);
auto local_tracklocks = mem_rmalloc<mngs_track_lock>(5000);
// Do textures
int done = 0;
while (!done) {

View File

@ -1247,7 +1247,7 @@ int mng_GetGuaranteedTexturePage(char *name, CFILE *infile) {
if (i != -1)
return i;
mngs_texture_page *texpage = (mngs_texture_page *)mem_malloc(sizeof(*texpage));
auto texpage = mem_rmalloc<mngs_texture_page>();
// Not in memory. Load it from the table file. Start searching from the
// current spot in the open table file

View File

@ -1802,9 +1802,9 @@ int ReadNewModelFile(int polynum, CFILE *infile) {
pm->submodel[i].num_key_angles = nframes;
}
pm->submodel[i].keyframe_axis = (vector *)mem_malloc((pm->submodel[i].num_key_angles + 1) * sizeof(vector));
pm->submodel[i].keyframe_axis = mem_rmalloc<vector>(pm->submodel[i].num_key_angles + 1);
pm->submodel[i].keyframe_angles = (int *)mem_malloc((pm->submodel[i].num_key_angles + 1) * sizeof(int));
pm->submodel[i].keyframe_matrix = (matrix *)mem_malloc((pm->submodel[i].num_key_angles + 1) * sizeof(matrix));
pm->submodel[i].keyframe_matrix = mem_rmalloc<matrix>(pm->submodel[i].num_key_angles + 1);
if (timed) {
pm->submodel[i].rot_start_time = (int *)mem_malloc((pm->submodel[i].num_key_angles + 1) * sizeof(int));
ASSERT(pm->submodel[i].rot_start_time != nullptr);
@ -1892,7 +1892,7 @@ int ReadNewModelFile(int polynum, CFILE *infile) {
} else
pm->submodel[i].num_key_pos = nframes;
pm->submodel[i].keyframe_pos = (vector *)mem_malloc((pm->submodel[i].num_key_pos + 1) * sizeof(vector));
pm->submodel[i].keyframe_pos = mem_rmalloc<vector>(pm->submodel[i].num_key_pos + 1);
ASSERT(pm->submodel[i].keyframe_pos != nullptr);
if (timed) {