int getdtablesize (void) { if (dtablesize == 0) { /* We are looking for the number N such that the valid file descriptors are 0..N-1. It can be obtained through a loop as follows: { int fd; for (fd = 3; fd < 65536; fd++) if (dup2 (0, fd) == -1) break; return fd; } On Windows XP, the result is 2048. The drawback of this loop is that it allocates memory for a libc internal array that is never freed. The number N can also be obtained as the upper bound for _getmaxstdio (). _getmaxstdio () returns the maximum number of open FILE objects. The sanity check in _setmaxstdio reveals the maximum number of file descriptors. This too allocates memory, but it is freed when we call _setmaxstdio with the original value. */ int orig_max_stdio = _getmaxstdio (); unsigned int bound; for (bound = 0x10000; _setmaxstdio (bound) < 0; bound = bound / 2) ; _setmaxstdio (orig_max_stdio); dtablesize = bound; } return dtablesize; }
ccReturn ccSysinfoInitialize(void) { SYSTEM_INFO sysinfo; MEMORYSTATUSEX memstat; memstat.dwLength = sizeof(MEMORYSTATUSEX); GlobalMemoryStatusEx(&memstat); ccAssert(_ccSysinfo == NULL); ccMalloc(_ccSysinfo, sizeof(ccSysinfo)); if(GetPhysicallyInstalledSystemMemory(&_ccSysinfo->ramTotal) == FALSE) goto fail; _ccSysinfo->ramTotal <<= 10; GetSystemInfo(&sysinfo); _ccSysinfo->processorCount = sysinfo.dwNumberOfProcessors; _ccSysinfo->fileMaxOpen = _getmaxstdio(); return CC_SUCCESS; fail: free(_ccSysinfo); return CC_FAIL; }
/* --------------------------------------------------------- init_globals --- */ void RTcmix::init_globals(bool fromMain, const char *defaultDSOPath) { Option::init(); if (defaultDSOPath && defaultDSOPath[0]) Option::dsoPathPrepend(defaultDSOPath); if (fromMain) { #ifndef MAXMSP Option::readConfigFile(Option::rcName()); Option::exitOnError(true); // we do this no matter what is in config file #else Option::exitOnError(false); #endif rtInteractive = 0; } else { SR = 44100.0; // what the heck... Option::print(0); Option::reportClipping(false); } RTBUFSAMPS = (int) Option::bufferFrames(); /* modifiable with rtsetparams */ rtHeap = new heap; rtQueue = new RTQueue[MAXBUS*3]; #ifdef MULTI_THREAD taskManager = new TaskManager; mixVector.reserve(MAXBUS); #endif for (int i = 0; i < MAXBUS; i++) { AuxToAuxPlayList[i] = -1; /* The playback order for AUX buses */ ToOutPlayList[i] = -1; /* The playback order for AUX buses */ ToAuxPlayList[i] =-1; /* The playback order for AUX buses */ } #ifdef MINGW max_input_fds = _getmaxstdio(); #else max_input_fds = sysconf(_SC_OPEN_MAX); #endif if (max_input_fds == -1) // call failed max_input_fds = 128; // what we used to hardcode else max_input_fds -= RESERVE_INPUT_FDS; inputFileTable = new InputFile[max_input_fds]; init_buf_ptrs(); if (Option::autoLoad()) { const char *dsoPath = Option::dsoPath(); if (strlen(dsoPath) == 0) registerDSOs(SHAREDLIBDIR); else registerDSOs(dsoPath); } }
/** * Described in header. */ void closefrom(int lowfd) { char fd_dir[PATH_MAX]; int maxfd, fd, len; /* try to close only open file descriptors on Linux... */ len = snprintf(fd_dir, sizeof(fd_dir), "/proc/%u/fd", getpid()); if (len > 0 && len < sizeof(fd_dir) && access(fd_dir, F_OK) == 0) { enumerator_t *enumerator = enumerator_create_directory(fd_dir); if (enumerator) { char *rel; while (enumerator->enumerate(enumerator, &rel, NULL, NULL)) { fd = atoi(rel); if (fd >= lowfd) { close(fd); } } enumerator->destroy(enumerator); return; } } /* ...fall back to closing all fds otherwise */ #ifdef WIN32 maxfd = _getmaxstdio(); #else maxfd = (int)sysconf(_SC_OPEN_MAX); #endif if (maxfd < 0) { maxfd = 256; } for (fd = lowfd; fd < maxfd; fd++) { close(fd); } }
int getdtablesize() { return _getmaxstdio(); }
void testMaxHandlersOfFiles() { printf( "maxstdio: %d\n", _getmaxstdio()); _setmaxstdio(2048); printf( "maxstdio: %d\n", _getmaxstdio()); }
/** * Described in header. */ void closefrom(int low_fd) { int max_fd, dir_fd, fd; /* try to close only open file descriptors on Linux... */ #if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H) /* By directly using a syscall we avoid any calls that might be unsafe after * fork() (e.g. malloc()). */ char buffer[sizeof(struct linux_dirent64)]; struct linux_dirent64 *entry; int offset, len; dir_fd = open("/proc/self/fd", O_RDONLY); if (dir_fd != -1) { while ((len = syscall(SYS_getdents64, dir_fd, buffer, sizeof(buffer))) > 0) { for (offset = 0; offset < len; offset += entry->d_reclen) { entry = (struct linux_dirent64*)(buffer + offset); if (!isdigit(entry->d_name[0])) { continue; } fd = atoi(entry->d_name); if (fd != dir_fd && fd >= low_fd) { close(fd); } } } close(dir_fd); return; } #else /* !defined(__linux__) || !defined(HAVE_SYS_SYSCALL_H) */ /* This is potentially unsafe when called after fork() in multi-threaded * applications. In particular opendir() will require an allocation. * Depends on how the malloc() implementation handles such situations. */ DIR *dir; struct dirent *entry; #ifndef HAVE_DIRFD /* if we don't have dirfd() lets close the lowest FD and hope it gets reused * by opendir() */ close(low_fd); dir_fd = low_fd++; #endif dir = opendir(FD_DIR); if (dir) { #ifdef HAVE_DIRFD dir_fd = dirfd(dir); #endif while ((entry = readdir(dir))) { if (!isdigit(entry->d_name[0])) { continue; } fd = atoi(entry->d_name); if (fd != dir_fd && fd >= low_fd) { close(fd); } } closedir(dir); return; } #endif /* defined(__linux__) && defined(HAVE_SYS_SYSCALL_H) */ /* ...fall back to closing all fds otherwise */ #ifdef WIN32 max_fd = _getmaxstdio(); #else max_fd = (int)sysconf(_SC_OPEN_MAX); #endif if (max_fd < 0) { max_fd = 256; } for (fd = low_fd; fd < max_fd; fd++) { close(fd); } }
static PyObject * winutil_get_max_stdio(PyObject *self, PyObject *args) { return Py_BuildValue("i", _getmaxstdio()); }
int pdc_get_maxfilehandles(void) { return _getmaxstdio(); }