📄 shared.h
字号:
/*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************///// This file is part of the Linux Multi-Threaded Environment.//// Copyright (C) 2000-2001 The Linux MTE Team.//#ifndef _LMTE_THREAD_SHARED_H#define _LMTE_THREAD_SHARED_H#include "thread.h"#include "thread_list.h"#include <string>extern "C" {# include <sys/ipc.h># include <sys/shm.h>};namespace cpp_threads { /** * 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@nissamedia.net> * @short Maintain shared memory. */ class SharedMemory { public: struct mem_entry { size_t s_size; void * s_data; }; struct storage { int s_id; int s_mid; key_t s_key; char * s_file; int s_proj; void * s_ptr; size_t s_page; }; typedef list<storage>::iterator sto_iterator; typedef list<mem_entry>::iterator mem_iterator; private: int _perm; int _blocks; std::string _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: SharedMemory(); SharedMemory(const std::string&); ~SharedMemory(); /** * Create a new branch from the main project tree. * * @param s The branch name. * @return An integer identifying the project. */ int createProj(const std::string&); /** * 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 makeKey(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 getKey(const std::string&,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 changeProj(const std::string&); /** * Change the permissions, that are given to newly * allocated shared memory pages. * * @param p The permissions to give. */ void changePerm(int); /** * Cleanup all memory, and lock the allocator to prohibit any * other access. */ void cleanup(); /** * This is the global allocator object. */ static SharedMemory share; };};#endif /* _LMTE_THREAD_SHARED_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -