2626#define UNICODE_HYPHEN 0x2010
2727#define UNICODE_NB_HYPHEN 0x2011
2828
29+ #ifdef USE_ATOMIC_REFCOUNT
30+ #include < atomic>
31+ #endif
2932
3033
3134// / strlen for lChar16
@@ -111,13 +114,20 @@ struct lstring8_chunk_t {
111114 friend class lString16 ;
112115 friend struct lstring_chunk_slice_t ;
113116public:
114- lstring8_chunk_t (lChar8 * _buf8) : buf8(_buf8), size(1 ), len(0 ), nref(1 ) {}
117+ lstring8_chunk_t (lChar8 * _buf8) : buf8(_buf8), size(1 ), len(0 )
118+ {
119+ refCount = 1 ;
120+ }
115121 const lChar8 * data8 () const { return buf8; }
116122private:
117123 lChar8 * buf8; // z-string
118124 lInt32 size; // 0 for free chunk
119125 lInt32 len; // count of chars in string
120- int nref; // reference counter
126+ #ifdef USE_ATOMIC_REFCOUNT
127+ std::atomic_int refCount; // atomic reference counter
128+ #else
129+ int refCount; // reference counter
130+ #endif
121131
122132 lstring8_chunk_t () {}
123133
@@ -132,13 +142,20 @@ struct lstring16_chunk_t {
132142 friend class lString16 ;
133143 friend struct lstring_chunk_slice_t ;
134144public:
135- lstring16_chunk_t (lChar16 * _buf16) : buf16(_buf16), size(1 ), len(0 ), nref(1 ) {}
145+ lstring16_chunk_t (lChar16 * _buf16) : buf16(_buf16), size(1 ), len(0 )
146+ {
147+ refCount = 1 ;
148+ }
136149 const lChar16 * data16 () const { return buf16; }
137150private:
138151 lChar16 * buf16; // z-string
139152 lInt32 size; // 0 for free chunk
140153 lInt32 len; // count of chars in string
141- int nref; // reference counter
154+ #ifdef USE_ATOMIC_REFCOUNT
155+ std::atomic_int refCount; // atomic reference counter
156+ #else
157+ int refCount; // reference counter
158+ #endif
142159
143160 lstring16_chunk_t () {}
144161
@@ -164,6 +181,7 @@ namespace fmt {
164181 };
165182}
166183
184+
167185/* *
168186 \brief lChar8 string
169187
@@ -206,8 +224,24 @@ class lString8
206224 static lstring_chunk_t * EMPTY_STR_8 ;
207225 void alloc (size_type sz);
208226 void free ();
209- inline void addref () const { ++pchunk->nref ; }
210- inline void release () { if (--pchunk->nref ==0 ) free (); }
227+ inline void addref () const {
228+ #ifdef USE_ATOMIC_REFCOUNT
229+ pchunk->refCount .fetch_add (1 );
230+ #else
231+ ++pchunk->refCount ;
232+ #endif
233+ }
234+ inline void release () {
235+ #ifdef USE_ATOMIC_REFCOUNT
236+ if (pchunk->refCount .fetch_sub (1 ) <= 1 )
237+ free ();
238+ #else
239+ if (--pchunk->refCount ==0 ) free ();
240+ #endif
241+ }
242+ inline int refCount () {
243+ return pchunk->refCount ;
244+ }
211245 explicit lString8 (lstring_chunk_t * chunk) : pchunk(chunk) { addref (); }
212246public:
213247 // / default constrictor
@@ -350,7 +384,7 @@ class lString8
350384 // / ensures that reference count is 1
351385 void lock ( size_type newsize );
352386 // / returns pointer to modifable string buffer
353- value_type * modify () { if (pchunk-> nref >1 ) lock (pchunk->len ); return pchunk->buf8 ; }
387+ value_type * modify () { if (refCount () >1 ) lock (pchunk->len ); return pchunk->buf8 ; }
354388 // / clear string
355389 void clear () { release (); pchunk = EMPTY_STR_8 ; addref (); }
356390 // / clear string, set buffer size
@@ -441,8 +475,24 @@ class lString16
441475 static lstring_chunk_t * EMPTY_STR_16 ;
442476 void alloc (size_type sz);
443477 void free ();
444- inline void addref () const { ++pchunk->nref ; }
445- inline void release () { if (--pchunk->nref ==0 ) free (); }
478+ inline void addref () const {
479+ #ifdef USE_ATOMIC_REFCOUNT
480+ pchunk->refCount .fetch_add (1 );
481+ #else
482+ ++pchunk->refCount ;
483+ #endif
484+ }
485+ inline void release () {
486+ #ifdef USE_ATOMIC_REFCOUNT
487+ if (pchunk->refCount .fetch_sub (1 ) <= 1 )
488+ free ();
489+ #else
490+ if (--pchunk->refCount ==0 ) free ();
491+ #endif
492+ }
493+ inline int refCount () {
494+ return pchunk->refCount ;
495+ }
446496public:
447497 explicit lString16 (lstring_chunk_t * chunk) : pchunk(chunk) { addref (); }
448498 // / empty string constructor
@@ -606,7 +656,7 @@ class lString16
606656 // / resizes string, copies if several references exist
607657 void lock ( size_type newsize );
608658 // / returns writable pointer to string buffer
609- value_type * modify () { if (pchunk-> nref >1 ) lock (pchunk->len ); return pchunk->buf16 ; }
659+ value_type * modify () { if (refCount () >1 ) lock (pchunk->len ); return pchunk->buf16 ; }
610660 // / clears string contents
611661 void clear () { release (); pchunk = EMPTY_STR_16 ; addref (); }
612662 // / resets string, allocates space for specified amount of characters
0 commit comments