⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mm.3

📁 Apache V2.0.15 Alpha For Linuxhttpd-2_0_15-alpha.tar.Z
💻 3
📖 第 1 页 / 共 3 页
字号:
.Vb 3\& void    \fBmm_lib_error_set\fR(unsigned int, const char *str);\& char   *\fBmm_lib_error_get\fR(void);\& int     \fBmm_lib_version\fR(void);.Ve.SH "DESCRIPTION"The \fBMM\fR library is a 2-layer abstraction library which simplifies the usageof shared memory between forked (and this way strongly related) processesunder Unix platforms. On the first (lower) layer it hides all platformdependent implementation details (allocation and locking) when dealing withshared memory segments and on the second (higher) layer it provides ahigh-level \fImalloc\fR\|(3)\-style API for a convenient and well known way to workwith data-structures inside those shared memory segments. .PPThe abbreviation \fBMM\fR is historically and originally comes from the phrase``\fImemory mapped\fR'\*(R' as used by the POSIX.1 \fImmap\fR\|(2) function. Because thisfacility is internally used by this library on most platforms to establish theshared memory segments. .Sh "\s-1LIBRARY\s0 \s-1STRUCTURE\s0"This library is structured into three main APIs which are internally based oneach other:.Ip "\fBGlobal Malloc-Replacement \s-1API\s0\fR" 4This is the most high-level \s-1API\s0 which directly can be used as replacement \s-1API\s0for the \s-1POSIX\s0.1 memory allocation \s-1API\s0 (\fImalloc\fR\|(2) and friends). This isuseful when converting \fIheap\fR based data structures to \fIshared memory\fRbased data structures without the need to change the code dramatically.  Allwhich is needed is to prefix the \s-1POSIX\s0.1 memory allocation functions with`\f(CWMM_\fR\*(R', i.e. `\f(CWmalloc\fR\*(R' becomes `\f(CWMM_malloc\fR\*(R', `\f(CWstrdup\fR\*(R' becomes`\f(CWMM_strdup\fR\*(R', etc. This \s-1API\s0 internally uses just a global `\f(CWMM *\fR\*(R' pool forcalling the corresponding functions (those with prefix `\f(CWmm_\fR') of the\fIStandard Malloc-Style \s-1API\s0\fR..Ip "\fBStandard Malloc-Style \s-1API\s0\fR" 4This is the standard high-level memory allocation \s-1API\s0. Its interface issimilar to the \fIGlobal Malloc-Replacement \s-1API\s0\fR but it uses an explicit `\f(CWMM *\fR\*(R'pool to operate on. That is why every function of this \s-1API\s0 has an argument oftype `\f(CWMM *\fR\*(R' as its first argument. This \s-1API\s0 provides a comfortable way towork with small dynamically allocated shared memory chunks inside largestatically allocated shared memory segments. It is internally based on the\fILow-Level Shared Memory \s-1API\s0\fR for creating the underlaying shared memorysegment..Ip "\fBLow-Level Shared Memory \s-1API\s0\fR" 4This is the basis of the whole \fB\s-1MM\s0\fR library. It provides low-level functionsfor creating shared memory segments with mutual exclusion (in short \fImutex\fR)capabilities in a portable way. Internally the shared memory and mutexfacility is implemented in various platform-dependent ways. A list ofimplementation variants follows under the next topic..Sh "\s-1SHARED\s0 \s-1MEMORY\s0 \s-1IMPLEMENTATION\s0"Internally the shared memory facility is implemented in variousplatform-dependent ways. Each way has its own advantages and disadvantages(in addition to the fact that some variants aren't available at all on someplatforms). The \fB\s-1MM\s0\fR library's configuration procedure tries hard to make agood decision. The implemented variants are now given for overview andbackground reasons with their advantages and disadvantages and in an ascendingorder, i.e. the \fB\s-1MM\s0\fR configuration mechanism chooses the last available onein the list as the preferred variant..Ip "Classical mmap(2) on temporary file (\s-1MMFILE\s0)" 4\fIAdvantage:\fR maximum portable.\fIDisadvantage:\fR needs a temporary file on the filesystem..Ip "mmap(2) via \s-1POSIX\s0.1 shm_open(3) on temporary file (\s-1MMPOSX\s0)" 4\fIAdvantage:\fR standardized by \s-1POSIX\s0.1 and theoretically portable.\fIDisadvantage:\fR needs a temporary file on the filesystem and isis usually not available on existing Unix platform..Ip "\s-1SVR4-\s0style mmap(2) on \f(CW/dev/zero\fR device (\s-1MMZERO\s0)" 4\fIAdvantage:\fR widely available and mostly portable on \s-1SVR4\s0 platforms.\fIDisadvantage:\fR needs the \f(CW/dev/zero/\fR device and a \fImmap\fR\|(2)which supports memory mapping through this device..Ip "4.4BSD\-style mmap(2) via \f(CWMAP_ANON\fR facility (\s-1MMANON\s0)" 4\fIAdvantage:\fR does not need a temporary file or external device.\fIDisadvantage:\fR usually only available on \s-1BSD\s0 platforms and derivatives..Ip "SysV \s-1IPC\s0 shmget(2) (\s-1IPCSHM\s0)" 4\fIAdvantage:\fR does not need a temporary file or external device.\fIDisadvantage:\fR although available on mostly all modern Unix platforms, it hasstrong restrictions like the maximum size of a single shared memory segment (canbe as small as 100KB, but depends on the platform)..Sh "\s-1LOCKING\s0 \s-1IMPLEMENTATION\s0"As for the shared memory facility, internally the locking facility isimplemented in various platform-dependent ways. A short overview ofimplemented variants is given:.Ip "4.2BSD\-style flock(2) on temporary file (\s-1FLOCK\s0)" 4\fIAdvantage:\fR exists on a lot of platforms, especially on older Unixderivates.  \fIDisadvantage:\fR needs a temporary file on the filesystem and hasto re-open file-descriptors to it in \fIeach\fR\|(!) \fIfork\fR\|(2)'ed child process..Ip "SysV \s-1IPC\s0 semget(2) (\s-1IPCSEM\s0)" 4\fIAdvantage:\fR exists on a lot of platforms and does not need a temporary file.\fIDisadvantage:\fR an unmeant termination of the application leads to asemaphore leak because the facility does not allow a ``remove in advance'\*(R'trick (as the \s-1IPC\s0 shared memory facility does) for safe cleanups..Ip "\s-1SVR4-\s0style fcntl(2) on temporary file (\s-1FCNTL\s0)" 4\fIAdvantage:\fR exists on a lot of platforms and is also the most powerfulvariant (although not always the fastest one). \fIDisadvantage:\fR needs atemporary file..Sh "\s-1MEMORY\s0 \s-1ALLOCATION\s0 \s-1STRATEGY\s0"The memory allocation strategy the \fIStandard Malloc-Style \s-1API\s0\fR functions useinternally is the following:.Ip "\fBAllocation\fR" 4If a chunk of memory has to be allocated, the internal list of free chunksis searched for a minimal-size chunk which is larger or equal than the size ofthe to be allocated chunk (a \fIbest fit\fR strategy>). .SpIf a chunk is found which matches this best-fit criteria, but is still a lotlarger than the requested size, it is split into two chunks: One with exactlythe requested size (which is the resulting chunk given back) and one with theremaining size (which is immediately re-inserted into the list of freechunks). .SpIf no fitting chunk is found at all in the list of free chunks, a new one iscreated from the spare area of the shared memory segment until the segment isfull (in which case an \fIout of memory\fR error occurs)..Ip "\fBDeallocation\fR" 4If a chunk of memory has to be deallocated, it is inserted in sorted mannerinto the internal list of free chunks. The insertion operation automaticallymerges the chunk with a previous and/or a next free chunk if possible, i.e.if the free chunks stay physically seamless (one after another) in memory, toautomatically form larger free chunks out of smaller ones. .SpThis way the shared memory segment is automatically defragmented when memoryis deallocated..PPThis strategy reduces memory waste and fragmentation caused by small andfrequent allocations and deallocations to a minimum. .PPThe internal implementation of the list of free chunks is not speciallyoptimized (for instance by using binary search trees or even \fIsplay\fR trees,etc), because it is assumed that the total amount of entries in the list offree chunks is always small (caused both by the fact that shared memorysegments are usually a lot smaller than heaps and the fact that we alwaysdefragment by merging the free chunks if possible)..SH "API FUNCTIONS"In the following all API functions are described in detail.The order directly follows the one in the \fBSYNOPSIS\fR..Sh "Global Malloc-Replacement \s-1API\s0".Ip "int \fBMM_create\fR(size_t \fIsize\fR, const char *\fIfile\fR);" 4This initializes the global shared memory pool with \fIsize\fR and \fIfile\fR andhas to be called \fIbefore\fR any \fIfork\fR\|(2) operations are performed by theapplication..Ip "int \fBMM_permission\fR(mode_t \fImode\fR, uid_t \fIowner\fR, gid_t \fIgroup\fR);" 4This sets the filesystem \fImode\fR, \fIowner\fR and \fIgroup\fR for the global sharedmemory pool (has effects only if the underlaying shared memory segmentimplementation is actually based on external auxiliary files).  The argumentsare directly passed through to \fIchmod\fR\|(2) and \fIchown\fR\|(2)..Ip "void \fBMM_destroy\fR(void);" 4This destroys the global shared memory pool and should be called \fIafter\fR allchild processes were killed..Ip "int \fBMM_lock\fR(mm_lock_mode \fImode\fR);" 4This locks the global shared memory pool for the current process in order toperform either shared/read-only (\fImode\fR is \f(CWMM_LOCK_RD\fR) orexclusive/read-write (\fImode\fR is \f(CWMM_LOCK_RW\fR) critical operations inside theglobal shared memory pool..Ip "int \fBMM_unlock\fR(void);" 4This unlocks the global shared memory pool for the current process after thecritical operations were performed inside the global shared memory pool..Ip "void *\fBMM_malloc\fR(size_t \fIsize\fR);" 4Identical to the \s-1POSIX\s0.1 \fImalloc\fR\|(3) function but instead of allocatingmemory from the \fIheap\fR it allocates it from the global shared memory pool..Ip "void \fBMM_free\fR(void *\fIptr\fR);" 4Identical to the \s-1POSIX\s0.1 \fIfree\fR\|(3) function but instead of deallocatingmemory in the \fIheap\fR it deallocates it in the global shared memory pool..Ip "void *\fBMM_realloc\fR(void *\fIptr\fR, size_t \fIsize\fR);" 4Identical to the \s-1POSIX\s0.1 \fIrealloc\fR\|(3) function but instead of reallocatingmemory in the \fIheap\fR it reallocates it inside the global shared memory pool..Ip "void *\fBMM_calloc\fR(size_t \fInumber\fR, size_t \fIsize\fR);" 4Identical to the \s-1POSIX\s0.1 \fIcalloc\fR\|(3) function but instead of allocating andinitializing memory from the \fIheap\fR it allocates and initializes it from theglobal shared memory pool..Ip "char *\fBMM_strdup\fR(const char *\fIstr\fR);" 4Identical to the \s-1POSIX\s0.1 \fIstrdup\fR\|(3) function but instead of creating thestring copy in the \fIheap\fR it creates it in the global shared memory pool..Ip "size_t \fBMM_sizeof\fR(const void *\fIptr\fR);" 4This function returns the size in bytes of the chunk starting at \fIptr\fR when\fIptr\fR was previously allocated with \fIMM_malloc\fR\|(3). The result is undefinedif \fIptr\fR was not previously allocated with \fIMM_malloc\fR\|(3)..Ip "size_t \fBMM_maxsize\fR(void);" 4This function returns the maximum size which is allowedas the first argument to the \fIMM_create\fR\|(3) function..Ip "size_t \fBMM_available\fR(void);" 4Returns the amount in bytes of still available (free) memory in the globalshared memory pool..Ip "char *\fBMM_error\fR(void);" 4Returns the last error message which occurred inside the \fB\s-1MM\s0\fR library..Sh "Standard Malloc-Style \s-1API\s0".Ip "\s-1MM\s0 *\fBmm_create\fR(size_t \fIsize\fR, const char *\fIfile\fR);" 4This creates a shared memory pool which has space for approximately a total of\fIsize\fR bytes with the help of \fIfile\fR. Here \fIfile\fR is a filesystem path to afile which need not to exist (and perhaps is never created because thisdepends on the platform and chosen shared memory and mutex implementation).The return value is a pointer to a \f(CWMM\fR structure which should be treated asopaque by the application. It describes the internals of the created sharedmemory pool. In case of an error \f(CWNULL\fR is returned.  A \fIsize\fR of 0 means toallocate the maximum allowed size which is platform dependent and is between afew \s-1KB\s0 and the soft limit of 64MB..Ip "int \fBmm_permission\fR(\s-1MM\s0 *\fImm\fR, mode_t \fImode\fR, uid_t \fIowner\fR, gid_t \fIgroup\fR);" 4This sets the filesystem \fImode\fR, \fIowner\fR and \fIgroup\fR for the shared memorypool \fImm\fR (has effects only when the underlaying shared memory segmentimplementation is actually based on external auxiliary files).  The argumentsare directly passed through to \fIchmod\fR\|(2) and \fIchown\fR\|(2)..Ip "void \fBmm_destroy\fR(\s-1MM\s0 *\fImm\fR);" 4This destroys the complete shared memory pool \fImm\fR and with it all chunkswhich were allocated in this pool. Additionally any created files on thefilesystem corresponding the to shared memory pool are unlinked..Ip "int \fBmm_lock\fR(\s-1MM\s0 *\fImm\fR, mm_lock_mode \fImode\fR);" 4This locks the shared memory pool \fImm\fR for the current process in order toperform either shared/read-only (\fImode\fR is \f(CWMM_LOCK_RD\fR) orexclusive/read-write (\fImode\fR is \f(CWMM_LOCK_RW\fR) critical operations inside theglobal shared memory pool..Ip "int \fBmm_unlock\fR(\s-1MM\s0 *\fImm\fR);" 4This unlocks the shared memory pool \fImm\fR for the current process aftercritical operations were performed inside the global shared memory pool..Ip "void *\fBmm_malloc\fR(\s-1MM\s0 *\fImm\fR, size_t \fIsize\fR);" 4This function allocates \fIsize\fR bytes from the shared memory pool \fImm\fR andreturns either a (virtual memory word aligned) pointer to it or \f(CWNULL\fR incase of an error (out of memory). It behaves like the \s-1POSIX\s0.1 \fImalloc\fR\|(3)function but instead of allocating memory from the \fIheap\fR it allocates itfrom the shared memory segment underlaying \fImm\fR..Ip "void \fBmm_free\fR(\s-1MM\s0 *\fImm\fR, void *\fIptr\fR);" 4This deallocates the chunk starting at \fIptr\fR in the shared memory pool \fImm\fR.It behaves like the \s-1POSIX\s0.1 \fIfree\fR\|(3) function but instead of deallocatingmemory from the \fIheap\fR it deallocates it from the shared memory segmentunderlaying \fImm\fR..Ip "void *\fBmm_realloc\fR(\s-1MM\s0 *\fImm\fR, void *\fIptr\fR, size_t \fIsize\fR);" 4This function reallocates the chunk starting at \fIptr\fR inside the sharedmemory pool \fImm\fR with the new size of \fIsize\fR bytes.  It behaves like the\s-1POSIX\s0.1 \fIrealloc\fR\|(3) function but instead of reallocating memory in the\fIheap\fR it reallocates it in the shared memory segment underlaying \fImm\fR..Ip "void *\fBmm_calloc\fR(\s-1MM\s0 *\fImm\fR, size_t \fInumber\fR, size_t \fIsize\fR);" 4This is similar to \fImm_malloc\fR\|(3), but additionally clears the chunk. It behaveslike the \s-1POSIX\s0.1 \fIcalloc\fR\|(3) function.  It allocates space for \fInumber\fRobjects, each \fIsize\fR bytes in length from the shared memory pool \fImm\fR.  Theresult is identical to calling \fImm_malloc\fR\|(3) with an argument of ``\fInumber\fR *\fIsize\fR'\*(R', with the exception that the allocated memory is initialized to nulbytes..Ip "char *\fBmm_strdup\fR(\s-1MM\s0 *\fImm\fR, const char *\fIstr\fR);" 4This function behaves like the \s-1POSIX\s0.1 \fIstrdup\fR\|(3) function.  It allocatessufficient memory inside the shared memory pool \fImm\fR for a copy of the string\fIstr\fR, does the copy, and returns a pointer to it.  The pointer maysubsequently be used as an argument to the function \fImm_free\fR\|(3). Ifinsufficient shared memory is available, \f(CWNULL\fR is returned..Ip "size_t \fBmm_sizeof\fR(const void *\fIptr\fR);" 4This function returns the size in bytes of the chunk starting at \fIptr\fR when\fIptr\fR was previously allocated with \fImm_malloc\fR\|(3). The result is undefinedwhen \fIptr\fR was not previously allocated with \fImm_malloc\fR\|(3)..Ip "size_t \fBmm_maxsize\fR(void);" 4This function returns the maximum size which is allowed as the first argumentto the \fImm_create\fR\|(3) function..Ip "size_t \fBmm_available\fR(\s-1MM\s0 *\fImm\fR);" 4Returns the amount in bytes of still available (free) memory in the shared memory pool \fImm\fR..Ip "char *\fBmm_error\fR(void);" 4Returns the last error message which occurred inside the \fB\s-1MM\s0\fR library..Ip "void \fBmm_display_info\fR(\s-1MM\s0 *\fImm\fR);" 4This is debugging function which displays a summary page for the shared memorypool \fImm\fR describing various internal sizes and counters..Sh "Low-Level Shared Memory \s-1API\s0".Ip "void *\fBmm_core_create\fR(size_t \fIsize\fR, const char *\fIfile\fR);" 4This creates a shared memory area which is at least \fIsize\fR bytes in size with

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -