40
40
#endif
41
41
42
42
/* --- C standard library ---*/
43
- #include < assert.h>
44
- #include < ctype.h>
45
- #include < errno.h>
46
- #include < stdarg.h>
47
- #include < stddef.h>
48
- #include < stdio.h>
49
- #include < stdlib.h>
50
- #include < string.h>
51
- #include < limits.h>
43
+ #include < cassert>
44
+ #include < cctype>
45
+ #include < cstdarg>
46
+ #include < cstddef>
47
+ #include < cstdio>
48
+ #include < cstdlib>
49
+ #include < cstring>
50
+ #include < climits>
52
51
#include < unistd.h>
53
52
#include < sched.h> /* sched_yield() */
54
53
#include < pthread.h>
@@ -268,7 +267,7 @@ int sw_printf(const char *format, ...);
268
267
269
268
#define sw_memset_zero (s, n ) memset(s, ' \0 ' , n)
270
269
271
- static sw_inline int sw_mem_equal (const void *v1, size_t s1, const void *v2, size_t s2) {
270
+ static inline int sw_mem_equal (const void *v1, size_t s1, const void *v2, size_t s2) {
272
271
return s1 == s2 && memcmp (v1, v2, s2) == 0 ;
273
272
}
274
273
@@ -284,15 +283,15 @@ static inline size_t swoole_strlcpy(char *dest, const char *src, size_t size) {
284
283
285
284
static inline char *swoole_strdup (const char *s) {
286
285
size_t l = strlen (s) + 1 ;
287
- char *p = ( char *) sw_malloc (l);
286
+ char *p = static_cast < char *>( sw_malloc (l) );
288
287
if (sw_likely (p)) {
289
288
memcpy (p, s, l);
290
289
}
291
290
return p;
292
291
}
293
292
294
- static inline char *swoole_strndup (const char *s, size_t n) {
295
- char *p = ( char *) sw_malloc (n + 1 );
293
+ static inline char *swoole_strndup (const char *s, const size_t n) {
294
+ char *p = static_cast < char *>( sw_malloc (n + 1 ) );
296
295
if (sw_likely (p)) {
297
296
strncpy (p, s, n)[n] = ' \0 ' ;
298
297
}
@@ -321,57 +320,53 @@ static inline const char *swoole_strnstr(const char *haystack,
321
320
const char *needle,
322
321
uint32_t needle_length) {
323
322
assert (needle_length > 0 );
324
- uint32_t i;
325
323
326
324
if (sw_likely (needle_length <= haystack_length)) {
327
- for (i = 0 ; i < haystack_length - needle_length + 1 ; i++) {
325
+ for (uint32_t i = 0 ; i < haystack_length - needle_length + 1 ; i++) {
328
326
if ((haystack[0 ] == needle[0 ]) && (0 == memcmp (haystack, needle, needle_length))) {
329
327
return haystack;
330
328
}
331
329
haystack++;
332
330
}
333
331
}
334
332
335
- return NULL ;
333
+ return nullptr ;
336
334
}
337
335
338
336
static inline const char *swoole_strncasestr (const char *haystack,
339
337
uint32_t haystack_length,
340
338
const char *needle,
341
339
uint32_t needle_length) {
342
340
assert (needle_length > 0 );
343
- uint32_t i;
344
341
345
342
if (sw_likely (needle_length <= haystack_length)) {
346
- for (i = 0 ; i < haystack_length - needle_length + 1 ; i++) {
343
+ for (uint32_t i = 0 ; i < haystack_length - needle_length + 1 ; i++) {
347
344
if ((haystack[0 ] == needle[0 ]) && (0 == strncasecmp (haystack, needle, needle_length))) {
348
345
return haystack;
349
346
}
350
347
haystack++;
351
348
}
352
349
}
353
350
354
- return NULL ;
351
+ return nullptr ;
355
352
}
356
353
357
354
static inline ssize_t swoole_strnpos (const char *haystack,
358
355
uint32_t haystack_length,
359
356
const char *needle,
360
357
uint32_t needle_length) {
361
358
assert (needle_length > 0 );
362
- const char *pos;
363
359
364
- pos = swoole_strnstr (haystack, haystack_length, needle, needle_length);
365
- return pos == NULL ? -1 : pos - haystack;
360
+ const char * pos = swoole_strnstr (haystack, haystack_length, needle, needle_length);
361
+ return pos == nullptr ? -1 : pos - haystack;
366
362
}
367
363
368
364
static inline ssize_t swoole_strrnpos (const char *haystack, const char *needle, uint32_t length) {
369
365
uint32_t needle_length = strlen (needle);
370
366
assert (needle_length > 0 );
371
- uint32_t i;
372
367
haystack += (length - needle_length);
373
368
374
- for (i = length - needle_length; i > 0 ; i--) {
369
+ for (uint32_t i = length - needle_length; i > 0 ; i--) {
375
370
if ((haystack[0 ] == needle[0 ]) && (0 == memcmp (haystack, needle, needle_length))) {
376
371
return i;
377
372
}
@@ -381,14 +376,12 @@ static inline ssize_t swoole_strrnpos(const char *haystack, const char *needle,
381
376
return -1 ;
382
377
}
383
378
384
- static inline void swoole_strtolower (char *str, int length) {
385
- char *c, *e;
386
-
387
- c = str;
388
- e = c + length;
379
+ static inline void swoole_strtolower (char *str, const int length) {
380
+ char *c = str;
381
+ const char *e = c + length;
389
382
390
383
while (c < e) {
391
- *c = tolower (*c);
384
+ *c = static_cast < char >( tolower (*c) );
392
385
c++;
393
386
}
394
387
}
@@ -503,17 +496,17 @@ void swoole_random_string(std::string &str, size_t size);
503
496
uint64_t swoole_random_int ();
504
497
size_t swoole_random_bytes (char *buf, size_t size);
505
498
506
- static sw_inline char *swoole_strlchr (char *p, char *last, char c) {
499
+ static inline char *swoole_strlchr (char *p, const char *last, char c) {
507
500
while (p < last) {
508
501
if (*p == c) {
509
502
return p;
510
503
}
511
504
p++;
512
505
}
513
- return NULL ;
506
+ return nullptr ;
514
507
}
515
508
516
- static sw_inline size_t swoole_size_align (size_t size, int pagesize) {
509
+ static inline size_t swoole_size_align (size_t size, int pagesize) {
517
510
return size + (pagesize - (size % pagesize));
518
511
}
519
512
@@ -564,18 +557,18 @@ int swoole_rand(int min, int max);
564
557
int swoole_system_random (int min, int max);
565
558
566
559
int swoole_version_compare (const char *version1, const char *version2);
567
- void swoole_print_backtrace (void );
568
- void swoole_print_backtrace_on_error (void );
560
+ void swoole_print_backtrace ();
561
+ void swoole_print_backtrace_on_error ();
569
562
char *swoole_string_format (size_t n, const char *format, ...);
570
563
bool swoole_get_env (const char *name, int *value);
571
564
int swoole_get_systemd_listen_fds ();
572
565
573
- void swoole_init (void );
574
- void swoole_clean (void );
575
- void swoole_exit (int __status );
566
+ void swoole_init ();
567
+ void swoole_clean ();
568
+ void swoole_exit (int _status );
576
569
pid_t swoole_fork (int flags);
577
- pid_t swoole_fork_exec (const std::function<void (void )> &child_fn);
578
- pid_t swoole_waitpid (pid_t __pid , int *__stat_loc , int __options );
570
+ pid_t swoole_fork_exec (const std::function<void ()> &child_fn);
571
+ pid_t swoole_waitpid (pid_t _pid , int *_stat_loc , int _options );
579
572
void swoole_thread_init (bool main_thread);
580
573
void swoole_thread_clean (bool main_thread);
581
574
void swoole_redirect_stdout (int new_fd);
@@ -606,18 +599,18 @@ int swoole_get_cpu_affinity(cpu_set_t *set);
606
599
int swoole_clock_realtime (struct timespec *t);
607
600
#endif
608
601
609
- static inline struct timespec swoole_time_until (int milliseconds) {
610
- struct timespec t;
602
+ static inline timespec swoole_time_until (time_t milliseconds) {
603
+ timespec t;
611
604
swoole_clock_realtime (&t);
612
605
613
- int sec = milliseconds / 1000 ;
614
- int msec = milliseconds - (sec * 1000 );
606
+ const time_t sec = milliseconds / 1000 ;
607
+ const time_t msec = milliseconds - (sec * 1000 );
615
608
616
609
t.tv_sec += sec;
617
610
t.tv_nsec += msec * 1000 * 1000 ;
618
611
619
612
if (t.tv_nsec > SW_NUM_BILLION) {
620
- int _sec = t.tv_nsec / SW_NUM_BILLION;
613
+ const time_t _sec = t.tv_nsec / SW_NUM_BILLION;
621
614
t.tv_sec += _sec;
622
615
t.tv_nsec -= _sec * SW_NUM_BILLION;
623
616
}
@@ -631,11 +624,11 @@ typedef long SessionId;
631
624
typedef long TaskId;
632
625
typedef uint8_t ReactorId;
633
626
typedef uint32_t WorkerId;
634
- typedef enum swEventType EventType;
635
- typedef enum swSocketType SocketType;
636
- typedef enum swFdType FdType;
637
- typedef enum swReturnCode ReturnCode;
638
- typedef enum swResultCode ResultCode;
627
+ typedef swEventType EventType;
628
+ typedef swSocketType SocketType;
629
+ typedef swFdType FdType;
630
+ typedef swReturnCode ReturnCode;
631
+ typedef swResultCode ResultCode;
639
632
640
633
struct Event {
641
634
int fd;
@@ -663,11 +656,11 @@ struct EventData {
663
656
DataHead info;
664
657
char data[SW_IPC_BUFFER_SIZE];
665
658
666
- uint32_t size () {
659
+ uint32_t size () const {
667
660
return sizeof (info) + len ();
668
661
}
669
662
670
- uint32_t len () {
663
+ uint32_t len () const {
671
664
return info.len ;
672
665
}
673
666
};
@@ -718,7 +711,7 @@ struct NameResolver {
718
711
};
719
712
std::function<std::string(const std::string &, Context *, void *)> resolve;
720
713
void *private_data;
721
- enum Type type;
714
+ Type type;
722
715
};
723
716
724
717
struct DnsServer {
@@ -790,7 +783,7 @@ struct Global {
790
783
std::string dirname (const std::string &file);
791
784
int hook_add (void **hooks, int type, const Callback &func, int push_back);
792
785
void hook_call (void **hooks, int type, void *arg);
793
- double microtime (void );
786
+ double microtime ();
794
787
} // namespace swoole
795
788
796
789
extern swoole::Global SwooleG; // Local Global Variable
@@ -802,22 +795,22 @@ static inline void swoole_set_last_error(int error) {
802
795
SwooleTG.error = error;
803
796
}
804
797
805
- static inline int swoole_get_last_error (void ) {
798
+ static inline int swoole_get_last_error () {
806
799
return SwooleTG.error ;
807
800
}
808
801
809
- static inline void swoole_clear_last_error (void ) {
802
+ static inline void swoole_clear_last_error () {
810
803
SwooleTG.error = 0 ;
811
804
}
812
805
813
- void swoole_clear_last_error_msg (void );
814
- const char *swoole_get_last_error_msg (void );
806
+ void swoole_clear_last_error_msg ();
807
+ const char *swoole_get_last_error_msg ();
815
808
816
- static inline int swoole_get_thread_id (void ) {
809
+ static inline int swoole_get_thread_id () {
817
810
return SwooleTG.id ;
818
811
}
819
812
820
- static inline int swoole_get_thread_type (void ) {
813
+ static inline int swoole_get_thread_type () {
821
814
return SwooleTG.type ;
822
815
}
823
816
0 commit comments