Skip to content

Commit 4066570

Browse files
CopilotJohnAmadis
andcommitted
Refactor memory region checks to use helper function and ternary pattern
- Created Dmod_IsAddressInRegion helper function to reduce code duplication - Use ternary pattern for caching: x = x != 0 ? x : Dmod_GetEnvAddress(...) - Each public function now just calls helper with its static variables Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
1 parent ba1a97c commit 4066570

File tree

1 file changed

+30
-77
lines changed

1 file changed

+30
-77
lines changed

src/system/if/dmod_if_rawmem.c

Lines changed: 30 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -263,37 +263,50 @@ static uintptr_t Dmod_GetEnvAddress(const char* EnvName)
263263
}
264264

265265
/**
266-
* @brief Check if address is in RAM
266+
* @brief Helper function to check if address is in a memory region
267267
*
268268
* @param Address Address to check
269+
* @param pStart Pointer to static variable for region start address
270+
* @param pEnd Pointer to static variable for region end address
271+
* @param envStart Name of environment variable for start address
272+
* @param envEnd Name of environment variable for end address
269273
*
270-
* @return true if address is in RAM, false otherwise
274+
* @return true if address is in the region, false otherwise
271275
*/
272-
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, bool, _IsRam, ( const void* Address ))
276+
static bool Dmod_IsAddressInRegion(const void* Address, uintptr_t* pStart, uintptr_t* pEnd,
277+
const char* envStart, const char* envEnd)
273278
{
274-
static uintptr_t ramStart = 0;
275-
static uintptr_t ramEnd = 0;
276-
277279
if (Address == NULL)
278280
{
279281
return false;
280282
}
281283

282-
// Read environment variables on first call
283-
if (ramEnd == 0)
284-
{
285-
ramStart = Dmod_GetEnvAddress("DMOD_RAM_START");
286-
ramEnd = Dmod_GetEnvAddress("DMOD_RAM_END");
287-
}
284+
// Read environment variables on first call (cache the values)
285+
*pStart = *pStart != 0 ? *pStart : Dmod_GetEnvAddress(envStart);
286+
*pEnd = *pEnd != 0 ? *pEnd : Dmod_GetEnvAddress(envEnd);
288287

289288
// If not configured, just check if address is not NULL
290-
if (ramEnd == 0)
289+
if (*pEnd == 0)
291290
{
292291
return true; // Address is not NULL, consider it potentially valid
293292
}
294293

295294
uintptr_t addr = (uintptr_t)Address;
296-
return (addr >= ramStart && addr < ramEnd);
295+
return (addr >= *pStart && addr < *pEnd);
296+
}
297+
298+
/**
299+
* @brief Check if address is in RAM
300+
*
301+
* @param Address Address to check
302+
*
303+
* @return true if address is in RAM, false otherwise
304+
*/
305+
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, bool, _IsRam, ( const void* Address ))
306+
{
307+
static uintptr_t ramStart = 0;
308+
static uintptr_t ramEnd = 0;
309+
return Dmod_IsAddressInRegion(Address, &ramStart, &ramEnd, "DMOD_RAM_START", "DMOD_RAM_END");
297310
}
298311

299312
/**
@@ -307,27 +320,7 @@ DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, bool, _IsRom, ( const void* Address )
307320
{
308321
static uintptr_t romStart = 0;
309322
static uintptr_t romEnd = 0;
310-
311-
if (Address == NULL)
312-
{
313-
return false;
314-
}
315-
316-
// Read environment variables on first call
317-
if (romEnd == 0)
318-
{
319-
romStart = Dmod_GetEnvAddress("DMOD_ROM_START");
320-
romEnd = Dmod_GetEnvAddress("DMOD_ROM_END");
321-
}
322-
323-
// If not configured, just check if address is not NULL
324-
if (romEnd == 0)
325-
{
326-
return true; // Address is not NULL, consider it potentially valid
327-
}
328-
329-
uintptr_t addr = (uintptr_t)Address;
330-
return (addr >= romStart && addr < romEnd);
323+
return Dmod_IsAddressInRegion(Address, &romStart, &romEnd, "DMOD_ROM_START", "DMOD_ROM_END");
331324
}
332325

333326
/**
@@ -341,27 +334,7 @@ DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, bool, _IsDma, ( const void* Address )
341334
{
342335
static uintptr_t dmaStart = 0;
343336
static uintptr_t dmaEnd = 0;
344-
345-
if (Address == NULL)
346-
{
347-
return false;
348-
}
349-
350-
// Read environment variables on first call
351-
if (dmaEnd == 0)
352-
{
353-
dmaStart = Dmod_GetEnvAddress("DMOD_DMA_START");
354-
dmaEnd = Dmod_GetEnvAddress("DMOD_DMA_END");
355-
}
356-
357-
// If not configured, just check if address is not NULL
358-
if (dmaEnd == 0)
359-
{
360-
return true; // Address is not NULL, consider it potentially valid
361-
}
362-
363-
uintptr_t addr = (uintptr_t)Address;
364-
return (addr >= dmaStart && addr < dmaEnd);
337+
return Dmod_IsAddressInRegion(Address, &dmaStart, &dmaEnd, "DMOD_DMA_START", "DMOD_DMA_END");
365338
}
366339

367340
/**
@@ -375,27 +348,7 @@ DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, bool, _IsExt, ( const void* Address )
375348
{
376349
static uintptr_t extStart = 0;
377350
static uintptr_t extEnd = 0;
378-
379-
if (Address == NULL)
380-
{
381-
return false;
382-
}
383-
384-
// Read environment variables on first call
385-
if (extEnd == 0)
386-
{
387-
extStart = Dmod_GetEnvAddress("DMOD_EXT_START");
388-
extEnd = Dmod_GetEnvAddress("DMOD_EXT_END");
389-
}
390-
391-
// If not configured, just check if address is not NULL
392-
if (extEnd == 0)
393-
{
394-
return true; // Address is not NULL, consider it potentially valid
395-
}
396-
397-
uintptr_t addr = (uintptr_t)Address;
398-
return (addr >= extStart && addr < extEnd);
351+
return Dmod_IsAddressInRegion(Address, &extStart, &extEnd, "DMOD_EXT_START", "DMOD_EXT_END");
399352
}
400353

401354
/**

0 commit comments

Comments
 (0)