From de5167a304b5e3b2db7462329334ac01d492d72c Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Thu, 1 Apr 2004 04:35:55 +0000 Subject: - Fixes all round - Uncontiguous FAT - Move to C (instead of C++) - Preparing for porting --- src/misc.c | 152 +++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 119 insertions(+), 33 deletions(-) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index 8939b73..4f48b1e 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1,38 +1,119 @@ -// -// AUTHOR -// N. Nielsen -// -// VERSION -// 0.7 -// -// LICENSE -// This software is in the public domain. -// -// The software is provided "as is", without warranty of any kind, -// express or implied, including but not limited to the warranties -// of merchantability, fitness for a particular purpose, and -// noninfringement. In no event shall the author(s) be liable for any -// claim, damages, or other liability, whether in an action of -// contract, tort, or otherwise, arising from, out of, or in connection -// with the software or the use or other dealings in the software. -// -// SUPPORT -// Send bug reports to: -// +/* + * AUTHOR + * N. Nielsen + * + * LICENSE + * This software is in the public domain. + * + * The software is provided "as is", without warranty of any kind, + * express or implied, including but not limited to the warranties + * of merchantability, fitness for a particular purpose, and + * noninfringement. In no event shall the author(s) be liable for any + * claim, damages, or other liability, whether in an action of + * contract, tort, or otherwise, arising from, out of, or in connection + * with the software or the use or other dealings in the software. + * + * SUPPORT + * Send bug reports to: + */ + +#include +#include +#include +#include + +#include "usuals.h" +#include "compat.h" +#include "memref.h" +#include "locks.h" -#include "memref.h" -#include "malloc.h" -#include "assert.h" +/* These locks are used to signify which */ +struct drivelock +{ + uint64 beg; + uint64 end; +}; + +void addLocationLock(drivelocks* locks, uint64 beg, uint64 end) +{ + uint32 i; + + if(locks->_count <= locks->_current) + { + locks->_count += 0x400; + locks->_locks = (struct drivelock*)realloc(locks->_locks, sizeof(struct drivelock) * locks->_count); + } -// WARNING!! Not thread safe or very efficient for large -// amounts of memory allocations + /* TODO: Implement a more efficient method here! */ + /* TODO: What happens when the above memory allocation fails? */ + if(locks->_locks) + { + /* Go through and check for a current lock we can tag onto */ + for(i = 0; i < locks->_current; i++) + { + if(INTERSECTS(locks->_locks[i].beg, locks->_locks[i].end, beg, end)) + { + locks->_locks[i].beg = min(locks->_locks[i].beg, beg); + locks->_locks[i].end = max(locks->_locks[i].end, end); + return; + } + } + + locks->_locks[locks->_current].beg = beg; + locks->_locks[locks->_current].end = end; + locks->_current++; + } +} + +bool checkLocationLock(drivelocks* locks, uint64 sec) +{ + uint32 i; + + if(locks->_locks) + { + /* Go through and check for a lock */ + for(i = 0; i < locks->_current; i++) + { + if(sec >= locks->_locks[i].beg && + sec < locks->_locks[i].end) + { + sec = locks->_locks[i].end; + return true; + } + } + } + + return false; +} + +#ifdef _DEBUG +void dumpLocationLocks(drivelocks* locks) +{ + uint32 i; + + for(i = 0; i < locks->_current; i++) + printf("%u\t%u\n", (uint32)locks->_locks[i].beg, (uint32)locks->_locks[i].end); + + printf("\n"); +} +#endif + + + + +/* + * WARNING!! Not thread safe or very efficient for large + * amounts of memory allocations + */ + +#ifdef _DEBUG const size_t kRefSig = 0x1F2F3F4F; void* _refalloc_dbg(size_t sz) { - // Allocate extra counter value before memory + /* Allocate extra counter value before memory */ size_t* pMem = (size_t*)malloc(sz * sizeof(size_t) * 2); if(pMem) @@ -44,10 +125,11 @@ void* _refalloc_dbg(size_t sz) return pMem; } +#endif void* _refalloc(size_t sz) { - // Allocate extra counter value before memory + /* Allocate extra counter value before memory */ size_t* pMem = (size_t*)malloc(sz * sizeof(size_t) * 1); if(pMem) @@ -59,11 +141,12 @@ void* _refalloc(size_t sz) return pMem; } +#ifdef _DEBUG void* _refadd_dbg(void* pBuff) { if(pBuff) { - // Increment the counter value + /* Increment the counter value */ size_t* pMem = (size_t*)pBuff - 2; assert(pMem[0] == kRefSig); pMem[1]++; @@ -71,21 +154,23 @@ void* _refadd_dbg(void* pBuff) return pBuff; } +#endif void* _refadd(void* pBuff) { if(pBuff) - // Increment the counter value + /* Increment the counter value */ ((size_t*)pBuff)[-1]++; return pBuff; } +#ifdef _DEBUG void _refrelease_dbg(void* pBuff) { if(pBuff) { - // Decrement the counter value + /* Decrement the counter value */ size_t* pMem = (size_t*)pBuff - 2; assert(pMem[0] == kRefSig); @@ -93,15 +178,16 @@ void _refrelease_dbg(void* pBuff) free(pMem); } } +#endif void _refrelease(void* pBuff) { if(pBuff) { - // Decrement the counter value + /* Decrement the counter value */ size_t* pMem = (size_t*)pBuff - 1; if(!--pMem[0]) free(pMem); } -} \ No newline at end of file +} -- cgit v1.2.3