Mercurial > hg > octave-nkf
comparison liboctave/oct-shlib.h @ 9958:80432f0ee895
improve octave_shlib for safer shared libs treatment
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 10 Dec 2009 09:14:47 +0100 |
parents | eb63fbe60fab |
children | 14ed68363284 |
comparison
equal
deleted
inserted
replaced
9957:59ed11557715 | 9958:80432f0ee895 |
---|---|
1 /* | 1 /* |
2 | 2 |
3 Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006, 2007, 2008 John W. Eaton | 3 Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006, 2007, 2008 John W. Eaton |
4 Copyright (C) 2009 VZLU Prague | |
4 | 5 |
5 This file is part of Octave. | 6 This file is part of Octave. |
6 | 7 |
7 Octave is free software; you can redistribute it and/or modify it | 8 Octave is free software; you can redistribute it and/or modify it |
8 under the terms of the GNU General Public License as published by the | 9 under the terms of the GNU General Public License as published by the |
22 | 23 |
23 #if !defined (octave_shlib_h) | 24 #if !defined (octave_shlib_h) |
24 #define octave_shlib_h 1 | 25 #define octave_shlib_h 1 |
25 | 26 |
26 #include <string> | 27 #include <string> |
28 #include <map> | |
27 | 29 |
28 #include "oct-time.h" | 30 #include "oct-time.h" |
29 | |
30 // This just provides a way to avoid infinite recursion when building | |
31 // octave_shlib objects. | |
32 | |
33 class | |
34 OCTAVE_API | |
35 octave_xshlib | |
36 { | |
37 public: | |
38 | |
39 octave_xshlib (void) { } | |
40 }; | |
41 | 31 |
42 class | 32 class |
43 OCTAVE_API | 33 OCTAVE_API |
44 octave_shlib | 34 octave_shlib |
45 { | 35 { |
36 public: // FIXME: make this class private? | |
37 | |
38 typedef std::string (*name_mangler) (const std::string&); | |
39 typedef void (*close_hook) (const std::string&); | |
40 | |
41 class shlib_rep | |
42 { | |
43 public: | |
44 | |
45 shlib_rep (void) | |
46 : count (1), file (), tm_loaded (time_t ()) { } | |
47 | |
48 protected: | |
49 | |
50 shlib_rep (const std::string& f); | |
51 | |
52 public: | |
53 | |
54 virtual ~shlib_rep (void) | |
55 { | |
56 instances.erase (file); | |
57 } | |
58 | |
59 virtual bool is_open (void) const | |
60 { return false; } | |
61 | |
62 virtual void *search (const std::string&, name_mangler = 0) | |
63 { return 0; } | |
64 | |
65 bool is_out_of_date (void) const; | |
66 | |
67 // This method will be overriden conditionally. | |
68 static shlib_rep *new_instance (const std::string& f); | |
69 | |
70 static shlib_rep *get_instance (const std::string& f, bool fake); | |
71 | |
72 octave_time time_loaded (void) const | |
73 { return tm_loaded; } | |
74 | |
75 std::string file_name (void) const | |
76 { return file; } | |
77 | |
78 void insert_hook_name (const std::string& name) const; | |
79 | |
80 void erase_hook_name (const std::string& name) const; | |
81 | |
82 size_t num_fcn_names (void) const { return fcn_names.size (); } | |
83 | |
84 void add_fcn_name (const std::string&); | |
85 | |
86 bool remove_fcn_name (const std::string&); | |
87 | |
88 void do_close_hook (close_hook cl_hook); | |
89 | |
90 public: | |
91 | |
92 int count; | |
93 | |
94 protected: | |
95 | |
96 void fake_reload (void); | |
97 | |
98 std::string file; | |
99 octave_time tm_loaded; | |
100 | |
101 // Set of hooked function names. | |
102 typedef std::map<std::string, size_t>::iterator fcn_names_iterator; | |
103 typedef std::map<std::string, size_t>::const_iterator fcn_names_const_iterator; | |
104 | |
105 std::map<std::string, size_t> fcn_names; | |
106 | |
107 static std::map<std::string, shlib_rep *> instances; | |
108 }; | |
109 | |
110 private: | |
111 | |
112 static shlib_rep nil_rep; | |
113 | |
46 public: | 114 public: |
47 | 115 |
48 typedef std::string (*name_mangler) (const std::string&); | 116 octave_shlib (void) : rep (&nil_rep) { rep->count++; } |
49 | 117 |
50 typedef void (*close_hook) (const std::string&); | 118 octave_shlib (const std::string& f, bool fake = true) |
51 | 119 : rep (shlib_rep::get_instance (f, fake)) { } |
52 octave_shlib (void) : relative (false), rep (make_shlib ()) { } | 120 |
53 | 121 ~octave_shlib (void) |
54 octave_shlib (const std::string& f) | |
55 : relative (false), rep (make_shlib ()) { open (f); } | |
56 | |
57 virtual ~octave_shlib (void) | |
58 { | 122 { |
59 if (rep && --rep->count == 0) | 123 if (--rep->count == 0) |
60 { | 124 delete rep; |
61 delete rep; | |
62 rep = 0; | |
63 } | |
64 } | 125 } |
65 | 126 |
66 octave_shlib (const octave_shlib& sl) | 127 octave_shlib (const octave_shlib& sl) |
67 { | 128 { |
68 rep = sl.rep; | 129 rep = sl.rep; |
84 } | 145 } |
85 | 146 |
86 bool operator == (const octave_shlib& sl) const | 147 bool operator == (const octave_shlib& sl) const |
87 { return (rep == sl.rep); } | 148 { return (rep == sl.rep); } |
88 | 149 |
89 operator bool () const { return is_open (); } | 150 operator bool () const { return rep->is_open (); } |
90 | 151 |
91 virtual void open (const std::string& f) { rep->open (f); } | 152 void open (const std::string& f) |
92 | 153 { *this = octave_shlib (f); } |
93 virtual void *search (const std::string& nm, name_mangler mangler = 0) | 154 |
94 { return rep->search (nm, mangler); } | 155 void close (close_hook cl_hook = 0) |
95 | 156 { |
96 virtual void close (close_hook cl_hook = 0) | 157 if (cl_hook) |
97 { rep->close (cl_hook); } | 158 rep->do_close_hook (cl_hook); |
98 | 159 |
99 virtual bool remove (const std::string& fcn_name) | 160 *this = octave_shlib (); |
100 { return rep->remove (fcn_name); } | 161 } |
101 | 162 |
102 virtual bool is_out_of_date (void) const | 163 void *search (const std::string& nm, name_mangler mangler = 0) const |
164 { | |
165 void *f = rep->search (nm, mangler); | |
166 if (f) | |
167 rep->add_fcn_name (nm); | |
168 | |
169 return f; | |
170 } | |
171 | |
172 void add (const std::string& name) | |
173 { rep->add_fcn_name (name); } | |
174 | |
175 bool remove (const std::string& name) | |
176 { return rep->remove_fcn_name (name); } | |
177 | |
178 size_t number_of_functions_loaded (void) const | |
179 { return rep->num_fcn_names (); } | |
180 | |
181 bool is_out_of_date (void) const | |
103 { return rep->is_out_of_date (); } | 182 { return rep->is_out_of_date (); } |
104 | 183 |
105 void mark_relative (void) { relative = true; } | 184 std::string file_name (void) const |
106 | |
107 bool is_relative (void) const { return relative; } | |
108 | |
109 virtual size_t number_of_functions_loaded (void) const | |
110 { return rep->number_of_functions_loaded (); } | |
111 | |
112 virtual std::string file_name (void) const | |
113 { return rep->file_name (); } | 185 { return rep->file_name (); } |
114 | 186 |
115 virtual octave_time time_loaded (void) const | 187 octave_time time_loaded (void) const |
116 { return rep->time_loaded (); } | 188 { return rep->time_loaded (); } |
117 | 189 |
118 protected: | 190 private: |
119 | 191 |
120 octave_shlib (const octave_xshlib&) : rep (0) { } | 192 shlib_rep *rep; |
121 | |
122 virtual bool is_open (void) const { return rep->is_open (); } | |
123 | |
124 static octave_shlib *make_shlib (void); | |
125 | |
126 // TRUE if this function was found from a relative path element. | |
127 bool relative; | |
128 | |
129 union | |
130 { | |
131 octave_shlib *rep; | |
132 int count; | |
133 }; | |
134 }; | 193 }; |
135 | 194 |
136 #endif | 195 #endif |
137 | 196 |
138 /* | 197 /* |