📄 shared.h
字号:
#ifndef __THREAD_SHARED_H#define __THREAD_SHARED_H#include "thread.h"#include "thread_list.h"extern "C" {# include <sys/ipc.h># include <sys/shm.h>};/** * This is a class that takes care of shared memory allocations * and access. * * By utilizing a malloc() like scheme, the class retrieves * shared pages from the kernel and installs them into a list * of free pages. These pages are usually far larger, then * the amount of memory a user will need, thus they will * fragmented and saved in a used list, where they can be * checked against, when a user returns them to the pool * * Memory that doesn't belong to the pool of shared memory, * is not returned to it... but ignored. * * @author Orn Hansen <oe.hansen@gamma.telenordia.se> * @short Maintain shared memory. */class shared_mem { public: struct mem_entry { size_t _size; void * _data; }; struct storage { int _id; int _mid; key_t _key; char * _file; int _proj; void * _ptr; size_t _page; }; typedef list<storage>::iterator sto_iterator; typedef list<mem_entry>::iterator mem_iterator; private: int _perm; int _blocks; char *_key_root; mutex _key_lock; // Exclusive access to keys. mutex _mem_lock; // Exclusive access to memory lists. struct mem_entry _mem_entry; struct storage _work_area; list<storage> _shared; list<mem_entry> _free_list; list<mem_entry> _used_list; mem_iterator getmem(sto_iterator,size_t); mem_iterator fillup(size_t,int); void fragment(mem_iterator,size_t); public: shared_mem(); shared_mem(const char *); ~shared_mem(); /** * Create a new branch from the main project tree. * * @param s The branch name. * @return An integer identifying the project. */ int create_proj(const char *); /** * Create a key project, that is connected to the * branch tree obtain from @ref #create_proj * * @param p The project id. * @return A key identifier that can be used to get shared memory. */ key_t make_key(int); /** * Obtain a key, which has alredy been created. The key * is calculated, based on a branch and project identifier. * * @param s The branch name. * @param i The project identifier. * @return Calculated key, or -1 if error. */ key_t get_key(const char *,int); /** * Allocate a selected sized memory, based on the given * key. * * @param k The key to allocate memory with. * @param s The size of memory to allocate. * @return A pointer to allocated memory, or 0 if error. */ void *keyalloc(key_t,size_t); /** * Allocate memory, and associate it with a given project * identifier. See @ref #create_proj * * @param s The size of memory to allocate. * @param i The project identifier, 0 = main project. * @return Allocated memory, or 0 if error. */ void *alloc(size_t,int); /** * Memory allocated should be returned to the memory pool. This will * enable programs to reuse it in another context, if needed. This is * not necessary though, as all shared memory mapped will be destroyed * on exit. * * @param p A pointer to the memory, that was allocated. */ void dealloc(void *); /** * Change the name of the main project... root name. * * @param s The name to give the root project. */ void change_proj(const char *); /** * Change the permissions, that are given to newly * allocated shared memory pages. * * @param p The permissions to give. */ void change_perm(int); /** * Cleanup all memory, and lock the allocator to prohibit any * other access. */ void cleanup(); /** * This is the global allocator object. */ static shared_mem share;};#endif /* __THREAD_SHARED_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -