/*** Deallocate ressources properly ***/ void cleanup(UBYTE *msg, int errcode) { CBClose(); close_port(); CloseMainWnd(1); CleanupLocale(); free_macros(); free_diskio_alloc(); /* ASL */ if(args.sa_Free) FreeVec(args.sa_ArgLst); if(DiskfontBase) CloseLibrary(DiskfontBase); if(LocaleBase) CloseLibrary((struct Library *)LocaleBase); if(AslBase) CloseLibrary(AslBase); if(IFFParseBase) CloseLibrary(IFFParseBase); if(UtilityBase) CloseLibrary((struct Library *)UtilityBase); if(KeymapBase) CloseLibrary(KeymapBase); if(GadToolsBase) CloseLibrary(GadToolsBase); if(GfxBase) CloseLibrary((struct Library *)GfxBase); if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase); if(msg) puts(msg); #if DEBUG /* Here can be compared good programs with the others :-) */ amem = AvailMem( MEMF_PUBLIC ); if(amem < bmem) printf("Possible memory lost of %d bytes\n", bmem-amem); #endif exit(errcode); }
/*** MAIN LOOP ***/ int main(int argc, char *argv[]) { #if DEBUG bmem = AvailMem( MEMF_PUBLIC ); #endif ParseArgs(&args, argc, argv); /* Look first if Jano isn't already running */ if( find_janoed( &args ) ) cleanup(0, RETURN_OK); /* Some global initialization */ init_searchtable(); /* Optionnal libraries */ AslBase = (struct Library *) OpenLibrary("asl.library", 36); LocaleBase = (struct LocaleBase *) OpenLibrary("locale.library", 38); DiskfontBase = (struct Library *) OpenLibrary("diskfont.library", 0); IFFParseBase = (struct Library *) OpenLibrary("iffparse.library",36); if(LocaleBase) InitLocale(); /* Localize the prog */ /* Open the required ROM libraries */ if( (IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 36)) && (GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 36)) && (GadToolsBase = (struct Library *) OpenLibrary("gadtools.library", 36)) && (KeymapBase = (struct Library *) OpenLibrary("keymap.library", 36)) && (UtilityBase = (struct UtilityBase *) OpenLibrary("utility.library", 36)) ) { init_macros(); set_default_prefs(&prefs, IntuitionBase->ActiveScreen); load_prefs(&prefs, NULL); /* See if it exists a config file */ sigport = create_port(); /* Create whether an empty project or an existing one */ if( ( edit = create_projects(NULL, args.sa_ArgLst, args.sa_NbArgs) ) ) { /* Open the main interface */ if(setup() == 0) { /* Makes edit project visible */ reshape_panel(edit); active_project(edit,TRUE); clear_brcorner(); dispatch_events(); } else cleanup(ErrMsg(ERR_NOGUI), RETURN_FAIL); } } else cleanup(ErrMsg(ERR_BADOS), RETURN_FAIL); /* Hope that all were well... */ cleanup(0, RETURN_OK); return 0; }
void InitC(void) { short i; List *list; Forbid(); CacheMax = (AvailMem(MEMF_CHIP) + AvailMem(MEMF_FAST)) / 4; if (CacheMax < 32768) CacheMax = 32768; CacheMaxFile = CacheMax >> 2; Permit(); NewList(&SuffixList); for (i = 0, list = CacheList; i < HSIZE; ++i, ++list) NewList(list); InitSemaphore(&SemLock); }
std::int64_t total_physical_ram() { #if defined TORRENT_BUILD_SIMULATOR return std::int64_t(4) * 1024 * 1024 * 1024; #else // figure out how much physical RAM there is in // this machine. This is used for automatically // sizing the disk cache size when it's set to // automatic. std::int64_t ret = 0; #ifdef TORRENT_BSD #ifdef HW_MEMSIZE int mib[2] = { CTL_HW, HW_MEMSIZE }; #else // not entirely sure this sysctl supports 64 // bit return values, but it's probably better // than not building int mib[2] = { CTL_HW, HW_PHYSMEM }; #endif std::size_t len = sizeof(ret); if (sysctl(mib, 2, &ret, &len, nullptr, 0) != 0) ret = 0; #elif defined TORRENT_WINDOWS MEMORYSTATUSEX ms; ms.dwLength = sizeof(MEMORYSTATUSEX); if (GlobalMemoryStatusEx(&ms)) ret = int(ms.ullTotalPhys); else ret = 0; #elif defined TORRENT_LINUX ret = sysconf(_SC_PHYS_PAGES); ret *= sysconf(_SC_PAGESIZE); #elif defined TORRENT_AMIGA ret = AvailMem(MEMF_PUBLIC); #endif #if TORRENT_USE_RLIMIT if (ret > 0) { struct rlimit r{}; if (getrlimit(rlimit_as, &r) == 0 && r.rlim_cur != rlim_infinity) { if (ret > std::int64_t(r.rlim_cur)) ret = std::int64_t(r.rlim_cur); } } #endif return ret; #endif // TORRENT_BUILD_SIMULATOR }
boost::uint64_t total_physical_ram() { // figure out how much physical RAM there is in // this machine. This is used for automatically // sizing the disk cache size when it's set to // automatic. boost::uint64_t ret = 0; #ifdef TORRENT_BSD #ifdef HW_MEMSIZE int mib[2] = { CTL_HW, HW_MEMSIZE }; #else // not entirely sure this sysctl supports 64 // bit return values, but it's probably better // than not building int mib[2] = { CTL_HW, HW_PHYSMEM }; #endif size_t len = sizeof(ret); if (sysctl(mib, 2, &ret, &len, NULL, 0) != 0) ret = 0; #elif defined TORRENT_WINDOWS MEMORYSTATUSEX ms; ms.dwLength = sizeof(MEMORYSTATUSEX); if (GlobalMemoryStatusEx(&ms)) ret = ms.ullTotalPhys; else ret = 0; #elif defined TORRENT_LINUX ret = sysconf(_SC_PHYS_PAGES); ret *= sysconf(_SC_PAGESIZE); #elif defined TORRENT_AMIGA ret = AvailMem(MEMF_PUBLIC); #endif #if TORRENT_USE_RLIMIT if (ret > 0) { struct rlimit r; if (getrlimit(RLIMIT_AS, &r) == 0 && r.rlim_cur != RLIM_INFINITY) { if (ret > r.rlim_cur) ret = r.rlim_cur; } } #endif return ret; }
// Indique quelle est la mémoire disponible unsigned long Memory_free(void) { // Memory is no longer relevant. If there is ANY problem or doubt here, // you can simply return 10*1024*1024 (10Mb), to make the "Pages"something // memory allocation functions happy. // However, it is still a good idea to make a proper function if you can... // If Grafx2 thinks the memory is full, weird things may happen. And if memory // ever becomes full and you're still saying there are 10MB free here, the // program will crash without saving any picture backup ! You've been warned... #if defined(__WIN32__) MEMORYSTATUS mstt; mstt.dwLength = sizeof(MEMORYSTATUS); GlobalMemoryStatus(&mstt); return mstt.dwAvailPhys; #elif defined(__macosx__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) int mib[2]; int maxmem; size_t len; mib[0] = CTL_HW; mib[1] = HW_USERMEM; len = sizeof(maxmem); sysctl(mib,2,&maxmem,&len,NULL,0); return maxmem; #elif defined(__HAIKU__) || defined(__BEOS__) int pages; system_info systemInfo; get_system_info(&systemInfo); pages = systemInfo.max_pages - systemInfo.used_pages; return pages * B_PAGE_SIZE; #elif defined(__AROS__) || defined(__MORPHOS__) || defined(__amigaos__) return AvailMem(MEMF_ANY); #elif defined(__linux__) struct sysinfo info; sysinfo(&info); return info.freeram*info.mem_unit; #else // AvailMem is misleading on os4 (os4 caches stuff in memory that you can still allocate) #warning "There is missing code there for your platform ! please check and correct :)" return 10*1024*1024; #endif }
/* Get a snapshot of current resources */ static struct TrackedResources *NewResourcesState(void) { struct TrackedResources *tr; tr = get_tr(); if (!tr) return NULL; /* flush */ FreeVec(AllocVec(~0ul/2, MEMF_ANY)); /* opencount-based stuff */ NEWLIST(&tr->opened); if (!AddOpenedResources(&tr->opened)) return NULL; /* memory */ tr->freeMem = AvailMem(MEMF_ANY); return tr; }
int main(int argc, char *argv[]) { printf("Welcome to Gravity!\n"); KPrintF("\n---- Session start -----\n"); ULONG chip = AvailMem(MEMF_CHIP); KPrintF("Chip free: %ld\n", chip); init_screen(); for (int i=0;i<=100000;i++) {} KPrintF("Are we alive ? %s\n","maybe"); cleanup(); return 0; }
int main(void) { IPTR args[NOOFARGS] = { (IPTR)FALSE, (IPTR)FALSE, (IPTR)FALSE, (IPTR)FALSE, (IPTR)FALSE }; struct RDArgs *rda; LONG error = 0; BOOL bPrintErr = TRUE; rda = ReadArgs(ARG_TEMPLATE, args, NULL); if (rda != NULL) { UWORD typeCount = 0; BOOL aChip = (BOOL)args[ARG_CHIP]; BOOL aFast = (BOOL)args[ARG_FAST]; BOOL aTotal = (BOOL)args[ARG_TOTAL]; BOOL aFlush = (BOOL)args[ARG_FLUSH]; aHuman = (BOOL)args[ARG_HUMAN]; if (aChip) { typeCount++; } if (aFast) { typeCount++; } if (aTotal) { typeCount++; } ULONG chip[4], fast[4], total[4]; if (typeCount > 1) { FPuts(Output(), "Only one of CHIP, FAST or TOTAL allowed\n"); bPrintErr = FALSE; FreeArgs(rda); return RETURN_FAIL; } else { if (aFlush) { APTR Mem; Mem = AllocMem(0x7ffffff0, MEMF_PUBLIC); if (Mem) FreeMem(Mem, 0x7ffffff0); } if(aChip) { chip[0] = AvailMem(MEMF_CHIP); if (printm(NULL, chip, 1) < 0) { error = RETURN_ERROR; } } else if(aFast) { fast[0] = AvailMem(MEMF_FAST); if (printm(NULL, fast, 1) < 0) { error = RETURN_ERROR; } } else if (aTotal) { total[0] = AvailMem(MEMF_ANY); if (printm(NULL, total, 1) < 0) { error = RETURN_ERROR; } } else { Forbid(); chip[0] = AvailMem(MEMF_CHIP); chip[2] = AvailMem(MEMF_CHIP | MEMF_TOTAL); chip[3] = AvailMem(MEMF_CHIP | MEMF_LARGEST); chip[1] = chip[2] - chip[0]; fast[0] = AvailMem(MEMF_FAST); fast[2] = AvailMem(MEMF_FAST | MEMF_TOTAL); fast[3] = AvailMem(MEMF_FAST | MEMF_LARGEST); fast[1] = fast[2] - fast[0]; total[0] = AvailMem(MEMF_ANY); total[2] = AvailMem(MEMF_ANY | MEMF_TOTAL); total[3] = AvailMem(MEMF_ANY | MEMF_LARGEST); total[1] = total[2] - total[0]; Permit(); if (PutStr("Type Available In-Use Maximum Largest\n") < 0 || printm("chip", chip, 4) < 0 || printm("fast", fast, 4) < 0 || printm("total", total, 4) < 0) { error = RETURN_ERROR; } } } FreeArgs(rda); } else { error = RETURN_FAIL; } if(error != RETURN_OK && bPrintErr) { PrintFault(IoErr(), "Avail"); } return error; }
int main(void) { AvailMem(MEMF_CLEAR); return 0; }
extern uint64_t tuklib_physmem(void) { uint64_t ret = 0; #if defined(_WIN32) || defined(__CYGWIN__) if ((GetVersion() & 0xFF) >= 5) { // Windows 2000 and later have GlobalMemoryStatusEx() which // supports reporting values greater than 4 GiB. To keep the // code working also on older Windows versions, use // GlobalMemoryStatusEx() conditionally. HMODULE kernel32 = GetModuleHandle("kernel32.dll"); if (kernel32 != NULL) { BOOL (WINAPI *gmse)(LPMEMORYSTATUSEX) = GetProcAddress( kernel32, "GlobalMemoryStatusEx"); if (gmse != NULL) { MEMORYSTATUSEX meminfo; meminfo.dwLength = sizeof(meminfo); if (gmse(&meminfo)) ret = meminfo.ullTotalPhys; } } } if (ret == 0) { // GlobalMemoryStatus() is supported by Windows 95 and later, // so it is fine to link against it unconditionally. Note that // GlobalMemoryStatus() has no return value. MEMORYSTATUS meminfo; meminfo.dwLength = sizeof(meminfo); GlobalMemoryStatus(&meminfo); ret = meminfo.dwTotalPhys; } #elif defined(__OS2__) unsigned long mem; if (DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM, &mem, sizeof(mem)) == 0) ret = mem; #elif defined(__DJGPP__) __dpmi_free_mem_info meminfo; if (__dpmi_get_free_memory_information(&meminfo) == 0 && meminfo.total_number_of_physical_pages != (unsigned long)-1) ret = (uint64_t)meminfo.total_number_of_physical_pages * 4096; #elif defined(__VMS) int vms_mem; int val = SYI$_MEMSIZE; if (LIB$GETSYI(&val, &vms_mem, 0, 0, 0, 0) == SS$_NORMAL) ret = (uint64_t)vms_mem * 8192; #elif defined(AMIGA) || defined(__AROS__) ret = AvailMem(MEMF_TOTAL); #elif defined(TUKLIB_PHYSMEM_AIX) ret = _system_configuration.physmem; #elif defined(TUKLIB_PHYSMEM_SYSCONF) const long pagesize = sysconf(_SC_PAGESIZE); const long pages = sysconf(_SC_PHYS_PAGES); if (pagesize != -1 && pages != -1) // According to docs, pagesize * pages can overflow. // Simple case is 32-bit box with 4 GiB or more RAM, // which may report exactly 4 GiB of RAM, and "long" // being 32-bit will overflow. Casting to uint64_t // hopefully avoids overflows in the near future. ret = (uint64_t)pagesize * (uint64_t)pages; #elif defined(TUKLIB_PHYSMEM_SYSCTL) int name[2] = { CTL_HW, #ifdef HW_PHYSMEM64 HW_PHYSMEM64 #else HW_PHYSMEM #endif }; union { uint32_t u32; uint64_t u64; } mem; size_t mem_ptr_size = sizeof(mem.u64); if (sysctl(name, 2, &mem.u64, &mem_ptr_size, NULL, 0) != -1) { // IIRC, 64-bit "return value" is possible on some 64-bit // BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64), // so support both. if (mem_ptr_size == sizeof(mem.u64)) ret = mem.u64; else if (mem_ptr_size == sizeof(mem.u32)) ret = mem.u32; } #elif defined(TUKLIB_PHYSMEM_GETSYSINFO) // Docs are unclear if "start" is needed, but it doesn't hurt // much to have it. int memkb; int start = 0; if (getsysinfo(GSI_PHYSMEM, (caddr_t)&memkb, sizeof(memkb), &start) != -1) ret = (uint64_t)memkb * 1024; #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC) struct pst_static pst; if (pstat_getstatic(&pst, sizeof(pst), 1, 0) != -1) ret = (uint64_t)pst.physical_memory * (uint64_t)pst.page_size; #elif defined(TUKLIB_PHYSMEM_GETINVENT_R) inv_state_t *st = NULL; if (setinvent_r(&st) != -1) { inventory_t *i; while ((i = getinvent_r(st)) != NULL) { if (i->inv_class == INV_MEMORY && i->inv_type == INV_MAIN_MB) { ret = (uint64_t)i->inv_state << 20; break; } } endinvent_r(st); } #elif defined(TUKLIB_PHYSMEM_SYSINFO) struct sysinfo si; if (sysinfo(&si) == 0) ret = (uint64_t)si.totalram * si.mem_unit; #endif return ret; }