void vmem_unmapfile() { dbg_printf("vmem_unmapfile()\n"); vmem_decommit(preserve,preserve_size); vmem_release(preserve,preserve_size); preserve = NULL; preserve_size = 0; #if 0 if (pview) { int i; i = UnmapViewOfFile(pview); dbg_printf("i = x%x\n",i); if (i == FALSE) os_error(); } #else // Note that under Windows 95, UnmapViewOfFile() seems to return random // values, not TRUE or FALSE. if (pview && UnmapViewOfFile(pview) == FALSE) os_error(); #endif pview = NULL; if (hFileMap != NULL && CloseHandle(hFileMap) != TRUE) os_error(); hFileMap = NULL; if (hFile != INVALID_HANDLE_VALUE && CloseHandle(hFile) != TRUE) os_error(); hFile = INVALID_HANDLE_VALUE; }
void vmem_setfilesize(unsigned long size) { if (hFile != INVALID_HANDLE_VALUE) { if (SetFilePointer(hFile,size,NULL,FILE_BEGIN) == 0xFFFFFFFF) os_error(); if (SetEndOfFile(hFile) == FALSE) os_error(); } }
__weak void rt_stk_check (void) { /* Check for stack overflow. */ if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) || (os_tsk.run->stack[0] != MAGIC_WORD)) { os_error (OS_ERR_STK_OVF); } }
void os_heapterm() { if (hHeap) { if (HeapDestroy(hHeap) == FALSE) os_error(); } }
void os_mbx_psh (P_MCB p_CB, void *p_msg) { /* Store the message to the mailbox queue or pass it to task directly. */ P_TCB p_TCB; /* Check if this was an 'isr_mbx_receive ()' post service request. */ if (p_CB->p_lnk != NULL && p_CB->isr_st == 2) { /* A task is waiting to send message, remove it from the waiting list. */ p_CB->isr_st = 0; p_TCB = os_get_first ((P_XCB)p_CB); p_TCB->ret_val = OS_R_OK; goto rdy; } /* A task is waiting for message, pass the message to task directly. */ if (p_CB->p_lnk != NULL && p_CB->count == 0) { p_TCB = os_get_first ((P_XCB)p_CB); p_TCB->p_msg = p_msg; p_TCB->ret_val = OS_R_MBX; rdy:p_TCB->state = READY; os_rmv_dly (p_TCB); os_put_prio (&os_rdy, p_TCB); } else { /* No task is waiting for message, store it to the mailbox queue. */ if (p_CB->count < p_CB->size) { p_CB->msg[p_CB->first] = p_msg; _incw (&p_CB->count); if (++p_CB->first == p_CB->size) { p_CB->first = 0; } } else { os_error (OS_ERR_MBX_OVF); } } }
void vmem_decommit(void *ptr,unsigned long size) { dbg_printf("vmem_decommit(ptr = %p, size = x%lx)\n",ptr,size); if (ptr) { if (!VirtualFree(ptr, size, MEM_DECOMMIT)) os_error(); } }
long tell_position (FILE *file) { long size; if ((size = ftell (file)) < 0) { os_error(); } return size; }
char * fc_strdup_notrim (const char *src, gfc_charlen_type src_len) { char *p = strndup (src, src_len); if (!p) os_error ("Memory allocation failed in fc_strdup"); return p; }
void auto_codec_open_decoder(const auto_codec & context, AVCodecID codec_id) { boost::mutex::scoped_lock lock(avcodec_mutex); AVCodec * codec = avcodec_find_decoder(codec_id); if (!codec) throw os_error("avcodec_find_decoder", ENOENT); os_check_error("avcodec_open", -avcodec_open2(context.get(), codec, NULL)); }
static os_error make_os_error(int err, const std::string& text = "") { std::stringstream msg; msg << "errno: " << err; if (!text.empty()) msg << " - " << text; return os_error(err, msg.str()); }
void os_freelibrary() { if (hdll) { if (FreeLibrary(hdll) != TRUE) os_error(); hdll = NULL; } }
void vmem_release(void *ptr,unsigned long size) { dbg_printf("vmem_release(ptr = %p, size = x%lx)\n",ptr,size); if (ptr) { if (!VirtualFree(ptr, 0, MEM_RELEASE)) os_error(); } }
void *os_getprocaddress(const char *funcname) { void *fp; //printf("getprocaddress('%s')\n",funcname); assert(hdll); fp = (void *)GetProcAddress(hdll,(LPCSTR)funcname); if (!fp) os_error(); return fp; }
void os_term() { if (hHeap) { if (HeapDestroy(hHeap) == FALSE) { hHeap = NULL; os_error(); } hHeap = NULL; } os_freelibrary(); }
/*--------------------------- rt_stk_check ----------------------------------*/ __weak void rt_stk_check (void) { /* Check for stack overflow. */ if (os_tsk.run->task_id == 0x01) { // TODO: For the main thread the check should be done against the main heap pointer } else { if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) || (os_tsk.run->stack[0] != MAGIC_WORD)) { os_error (OS_ERR_STK_OVF); } } }
void * xmallocarray (size_t nmemb, size_t size) { void *p; if (!nmemb || !size) size = nmemb = 1; else if (nmemb > SIZE_MAX / size) { errno = ENOMEM; os_error ("Integer overflow in xmallocarray"); } p = malloc (nmemb * size); if (!p) os_error ("Memory allocation failed in xmallocarray"); return p; }
void * xcalloc (size_t nmemb, size_t size) { if (nmemb * size == 0) nmemb = size = 1; void *p = calloc (nmemb, size); if (!p) os_error ("Allocating cleared memory failed"); return p; }
void rt_psq_enq (OS_ID entry, U32 arg) { /* Insert post service request "entry" into ps-queue. */ U32 idx; idx = rt_inc_qi (os_psq->size, &os_psq->count, &os_psq->first); if (idx < os_psq->size) { os_psq->q[idx].id = entry; os_psq->q[idx].arg = arg; } else { os_error (OS_ERR_FIFO_OVF); } }
void *vmem_reserve(void *ptr,unsigned long size) { void *p; #if 1 p = VirtualAlloc(ptr,size,MEM_RESERVE,PAGE_READWRITE); dbg_printf("vmem_reserve(ptr = %p, size = x%lx) = %p\n",ptr,size,p); #else dbg_printf("vmem_reserve(ptr = %p, size = x%lx) = %p\n",ptr,size,p); p = VirtualAlloc(ptr,size,MEM_RESERVE,PAGE_READWRITE); if (!p) os_error(); #endif return p; }
void * xmalloc (size_t n) { void *p; if (n == 0) n = 1; p = malloc (n); if (p == NULL) os_error ("Memory allocation failed"); return p; }
void * get_mem (size_t n) { void *p; #ifdef GFC_CLEAR_MEMORY p = (void *) calloc (1, n); #else p = (void *) malloc (n); #endif if (p == NULL) os_error ("Memory allocation failed"); return p; }
void auto_codec_open_encoder(const auto_codec & context, AVCodecID codec_id, int thread_count) { boost::mutex::scoped_lock lock(avcodec_mutex); AVCodec * codec = avcodec_find_encoder(codec_id); if (!codec) throw os_error("avcodec_find_encoder", ENOENT); #if LIBAVCODEC_VERSION_MAJOR > 52 || \ (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR >= 111) context.get()->thread_count = thread_count; context.get()->thread_type = FF_THREAD_SLICE; os_check_error("avcodec_open", -avcodec_open2(context.get(), codec, NULL)); #else os_check_error("avcodec_open", -avcodec_open2(context.get(), codec, NULL)); if (avcodec_thread_init(context.get(), thread_count)) throw std::runtime_error("avcodec_thread_init failed"); #endif }
static void * internal_realloc_size (void *mem, size_t size) { if (size == 0) { if (mem) free (mem); return NULL; } if (mem == 0) return get_mem (size); mem = realloc (mem, size); if (!mem) os_error ("Out of memory."); return mem; }
/* may return NULL */ char * fontpath_fullname(const char *filename) { FILE *fp; char *fullname = NULL; #if defined(PIPES) if (*filename == '<') { os_error(NO_CARET, "fontpath_fullname: No Pipe allowed"); } else #endif /* PIPES */ if ((fp = fopen(filename, "r")) == (FILE *) NULL) { /* try 'fontpath' variable */ char *tmppath, *path = NULL; while ((tmppath = get_fontpath()) != NULL) { TBOOLEAN subdirs = FALSE; path = gp_strdup(tmppath); if (path[strlen(path) - 1] == '!') { path[strlen(path) - 1] = '\0'; subdirs = TRUE; } /* if */ fullname = recursivefullname(path, filename, subdirs); if (fullname != NULL) { while (get_fontpath()); free(path); break; } free(path); } } else fullname = gp_strdup(filename); return fullname; }
void *globalrealloc(void *oldp,size_t newsize) { #if 0 void *p; // Initialize heap if (!hHeap) { hHeap = HeapCreate(0,0x10000,0); if (!hHeap) os_error(); } newsize = (newsize + 3) & ~3L; // round up to dwords if (newsize == 0) { if (oldp && HeapFree(hHeap,0,oldp) == FALSE) os_error(); p = NULL; } else if (!oldp) { p = newsize ? HeapAlloc(hHeap,0,newsize) : NULL; } else p = HeapReAlloc(hHeap,0,oldp,newsize); #elif 1 MEMORY_BASIC_INFORMATION query; void *p; BOOL bSuccess; if (!oldp) p = VirtualAlloc (NULL, newsize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); else { VirtualQuery (oldp, &query, sizeof(query)); if (!newsize) { p = NULL; goto L1; } else { newsize = (newsize + 0xFFFF) & ~0xFFFFL; if (query.RegionSize >= newsize) p = oldp; else { p = VirtualAlloc(NULL,newsize,MEM_COMMIT | MEM_RESERVE,PAGE_READWRITE); if (p) memcpy(p,oldp,query.RegionSize); L1: bSuccess = VirtualFree(oldp,query.RegionSize,MEM_DECOMMIT); if (bSuccess) bSuccess = VirtualFree(oldp,0,MEM_RELEASE); if (!bSuccess) os_error(); } } } #else void *p; if (!oldp) p = (void *)GlobalAlloc (0, newsize); else if (!newsize) { GlobalFree(oldp); p = NULL; } else p = (void *)GlobalReAlloc(oldp,newsize,0); #endif dbg_printf("globalrealloc(oldp = %p, size = x%x) = %p\n",oldp,newsize,p); return p; }
void __cdecl free(void *p) { if (p && HeapFree(hHeap,0,p) == FALSE) os_error(); }
void os_check_error(const char * function, int code) { if (code != 0) throw os_error(function, code); }
void os_heapinit() { hHeap = HeapCreate(0,0x10000,0); if (!hHeap) os_error(); }
void *vmem_mapfile(const char *filename,void *ptr,unsigned long size,int flag) { OSVERSIONINFO OsVerInfo; OsVerInfo.dwOSVersionInfoSize = sizeof(OsVerInfo); GetVersionEx(&OsVerInfo); dbg_printf("vmem_mapfile(filename = '%s', ptr = %p, size = x%lx, flag = %d)\n",filename,ptr,size,flag); hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) goto L1; // failure dbg_printf(" file created\n"); // Windows 95 does not implement PAGE_WRITECOPY (unfortunately treating // it just like PAGE_READWRITE). if (flag == 1 && OsVerInfo.dwPlatformId == 1) // Windows 95, 98, ME hFileMap = NULL; else hFileMap = CreateFileMapping(hFile,NULL, (flag == 1) ? PAGE_WRITECOPY : PAGE_READWRITE,0,size,NULL); if (hFileMap == NULL) // mapping failed { #if 1 // Win32s seems to always fail here. DWORD nbytes; dbg_printf(" mapping failed\n"); // If it was NT failing, assert. assert(OsVerInfo.dwPlatformId != VER_PLATFORM_WIN32_NT); // To work around, just read the file into memory. assert(flag == 1); preserve = vmem_reserve(ptr,size); if (!preserve) goto L2; if (!vmem_commit(preserve,size)) { vmem_release(preserve,size); preserve = NULL; goto L2; } preserve_size = size; if (!ReadFile(hFile,preserve,size,&nbytes,NULL)) os_error(); assert(nbytes == size); if (CloseHandle(hFile) != TRUE) os_error(); hFile = INVALID_HANDLE_VALUE; return preserve; #else // Instead of working around, we should find out why it failed. os_error(); #endif } else { dbg_printf(" mapping created\n"); pview = MapViewOfFileEx(hFileMap,flag ? FILE_MAP_COPY : FILE_MAP_WRITE, 0,0,size,ptr); if (pview == NULL) // mapping view failed { //os_error(); goto L3; } } dbg_printf(" pview = %p\n",pview); return pview; Terminate: if (UnmapViewOfFile(pview) == FALSE) os_error(); pview = NULL; L3: if (CloseHandle(hFileMap) != TRUE) os_error(); hFileMap = NULL; L2: if (CloseHandle(hFile) != TRUE) os_error(); hFile = INVALID_HANDLE_VALUE; L1: return NULL; // failure }
void os_loadlibrary(const char *dllname) { hdll = LoadLibrary((LPCTSTR) dllname); if (!hdll) os_error(); }