Skip to content

Commit 9101578

Browse files
author
zhengshuxin
committed
Optimize codes to avoid compiling warning by higher version g++.
1 parent 88b7bfc commit 9101578

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

lib_fiber/cpp/include/fiber/fiber_sem.hpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22
#include "fiber_cpp_define.hpp"
3-
#include <list>
3+
#include <cstdlib>
44
#include <cassert>
5+
#include <list>
56

67
struct ACL_FIBER_SEM;
78

@@ -151,7 +152,7 @@ class fiber_sbox2 : public box2<T> {
151152
, off_curr_(0)
152153
, off_next_(0)
153154
{
154-
box_ = (T*) malloc(sizeof(T) * capacity_);
155+
box_ = new T[capacity_];
155156
}
156157

157158
explicit fiber_sbox2(int buf)
@@ -160,10 +161,10 @@ class fiber_sbox2 : public box2<T> {
160161
, off_curr_(0)
161162
, off_next_(0)
162163
{
163-
box_ = (T*) malloc(sizeof(T) * capacity_);
164+
box_ = new T[capacity_];
164165
}
165166

166-
~fiber_sbox2() {}
167+
~fiber_sbox2() { delete []box_; }
167168

168169
// @override
169170
bool push(T t, bool dummy = false) {
@@ -184,8 +185,18 @@ class fiber_sbox2 : public box2<T> {
184185
off_next_ -= off_curr_;
185186
off_curr_ = 0;
186187
} else {
187-
capacity_ += 10000;
188-
box_ = (T*) realloc(box_, sizeof(T) * capacity_);
188+
size_t capacity = capacity_ + 10000;
189+
T* box = new T[capacity];
190+
for (size_t i = 0; i < capacity_; i++) {
191+
#if __cplusplus >= 201103L || defined(USE_CPP11)
192+
box[i] = std::move(box_[i]);
193+
#else
194+
box[i] = box_[i];
195+
#endif
196+
}
197+
delete []box_;
198+
box_ = box;
199+
capacity_ = capacity;
189200
}
190201
}
191202
box_[off_next_++] = t;

lib_fiber/cpp/include/fiber/fiber_tbox2.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ template<typename T>
4949
class fiber_tbox2 : public box2<T> {
5050
public:
5151
fiber_tbox2() : capacity_(10000) , off_curr_(0) , off_next_(0) {
52-
box_ = (T*) malloc(sizeof(T) * capacity_);
52+
box_ = new T[capacity_];
5353
}
5454

55-
~fiber_tbox2() { free(box_); }
55+
~fiber_tbox2() { delete []box_; }
5656

5757
/**
5858
* Clean up unconsumed messages in the message queue.
@@ -95,8 +95,19 @@ class fiber_tbox2 : public box2<T> {
9595
off_next_ -= off_curr_;
9696
off_curr_ = 0;
9797
} else {
98-
capacity_ += 10000;
99-
box_ = (T*) realloc(box_, sizeof(T) * capacity_);
98+
size_t capacity = capacity_ + 10000;
99+
T* box = new T[capacity];
100+
for (size_t i = 0; i < capacity_; i++) {
101+
#if __cplusplus >= 201103L || defined(USE_CPP11)
102+
box[i] = std::move(box_[i]);
103+
#else
104+
box[i] = box_[i];
105+
#endif
106+
}
107+
delete []box_;
108+
box_ = box;
109+
capacity_ = capacity;
110+
100111
}
101112
}
102113
#if __cplusplus >= 201103L || defined(USE_CPP11)

lib_fiber/samples-c++1x/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ all:
1515
@(cd client2; make)
1616
@(cd fiber_connect; make)
1717
@(cd fiber_tbox; make)
18+
@(cd fiber_box; make)
19+
@(cd fibers; make)
1820
cl clean:
1921
@(cd fiber; make clean)
2022
@(cd httpd; make clean)
@@ -32,5 +34,7 @@ cl clean:
3234
@(cd client2; make clean)
3335
@(cd fiber_connect; make clean)
3436
@(cd fiber_tbox; make clean)
37+
@(cd fiber_box; make clean)
38+
@(cd fibers; make clean)
3539

3640
rb rebuild: clean all

lib_fiber/samples-c++1x/Makefile_cpp.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CC = g++
22

33
CFLAGS = -c -g -W -Wall -Wcast-qual -Wcast-align \
4-
-Waggregate-return -Wno-long-long \
4+
-Wno-long-long \
55
-Wpointer-arith -Werror -Wshadow -O3 \
66
-D_REENTRANT -D_POSIX_PTHREAD_SEMANTICS -D_USE_FAST_MACRO
77

0 commit comments

Comments
 (0)