00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef PERSISTENT_FILE_ALLOCATOR_H
00018 #define PERSISTENT_FILE_ALLOCATOR_H
00019 #include "ace/pre.h"
00020 #include "ace/config-all.h"
00021
00022 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00023 #pragma once
00024 #endif
00025
00026 #include "orbsvcs/Notify/notify_serv_export.h"
00027 #include "orbsvcs/Notify/Random_File.h"
00028 #include "orbsvcs/Notify/Bit_Vector.h"
00029 #include "ace/Containers_T.h"
00030 #include "ace/Unbounded_Queue.h"
00031 #include "ace/Thread_Manager.h"
00032
00033 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00034
00035 namespace TAO_Notify
00036 {
00037
00038
00039
00040 class TAO_Notify_Serv_Export Persistent_Callback
00041 {
00042 public:
00043 virtual ~Persistent_Callback();
00044
00045
00046 virtual void persist_complete() = 0;
00047 };
00048
00049
00050
00051
00052
00053
00054
00055
00056 class TAO_Notify_Serv_Export Persistent_Storage_Block
00057 {
00058 public:
00059
00060 Persistent_Storage_Block(
00061 const size_t block_number,
00062 const size_t block_size);
00063
00064 Persistent_Storage_Block(const Persistent_Storage_Block& psb);
00065
00066 ~Persistent_Storage_Block();
00067
00068
00069
00070 void set_no_write();
00071
00072 bool get_no_write();
00073
00074
00075 void set_sync();
00076
00077 bool get_sync() const;
00078
00079
00080 size_t block_number() const;
00081
00082
00083 unsigned char* data() const;
00084
00085 void reassign_data(unsigned char* newptr, bool delete_old = false);
00086
00087
00088 size_t detach ();
00089
00090
00091 void set_callback(Persistent_Callback* callback);
00092
00093 Persistent_Callback* get_callback() const;
00094
00095
00096 void set_allocator_owns(bool allocator_owns = true);
00097
00098 bool get_allocator_owns() const;
00099
00100 private:
00101
00102 unsigned char* data_;
00103
00104 size_t block_number_;
00105
00106 bool no_write_;
00107
00108 bool sync_;
00109
00110 size_t block_size_;
00111
00112
00113 Persistent_Callback* callback_;
00114
00115 bool allocator_owns_;
00116 };
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 class TAO_Notify_Serv_Export Persistent_File_Allocator
00128 {
00129 public:
00130
00131 Persistent_File_Allocator();
00132
00133 ~Persistent_File_Allocator();
00134
00135 bool open (const ACE_TCHAR* filename,
00136 const size_t block_size = 512);
00137
00138
00139 void shutdown();
00140
00141
00142
00143 Persistent_Storage_Block* allocate();
00144
00145
00146 Persistent_Storage_Block* allocate_at(size_t block_number);
00147
00148
00149 Persistent_Storage_Block* allocate_nowrite();
00150
00151
00152 void used(size_t block_number);
00153
00154
00155 void free(size_t block_number);
00156
00157
00158 size_t block_size() const;
00159
00160
00161
00162
00163
00164
00165 bool read(Persistent_Storage_Block* psb);
00166
00167
00168
00169
00170
00171 bool write(Persistent_Storage_Block* psb);
00172
00173
00174 size_t file_size () const;
00175
00176 private:
00177
00178 void free_block(const size_t block_number);
00179
00180 bool allocate_block(size_t& block_number);
00181
00182
00183
00184 static ACE_THR_FUNC_RETURN thr_func(void * arg);
00185
00186 void shutdown_thread();
00187
00188 void run();
00189
00190 private:
00191 ACE_Thread_Manager thread_manager_;
00192 Random_File pstore_;
00193 Bit_Vector free_blocks_;
00194 ACE_Unbounded_Queue<Persistent_Storage_Block*> block_queue_;
00195 ACE_SYNCH_MUTEX lock_;
00196 ACE_SYNCH_MUTEX free_blocks_lock_;
00197 ACE_SYNCH_MUTEX queue_lock_;
00198 bool terminate_thread_;
00199 bool thread_active_;
00200 ACE_SYNCH_CONDITION wake_up_thread_;
00201 };
00202
00203 }
00204
00205 TAO_END_VERSIONED_NAMESPACE_DECL
00206
00207 #include "ace/post.h"
00208 #endif