queue_memcpy.cc
Go to the documentation of this file.
1 /*
2 # Software License Agreement (MIT License)
3 #
4 # Copyright (c) 2020, UFACTORY, Inc.
5 # All rights reserved.
6 #
7 # Author: Vinman <vinman.wen@ufactory.cc> <vinman.cub@gmail.com>
8 */
9 #include <string.h>
11 
12 QueueMemcpy::QueueMemcpy(long n, long n_size) {
13  total_ = n;
14  annode_size_ = n_size;
15  buf_ = new char[total_ * annode_size_];
16  flush();
17 }
18 
19 QueueMemcpy::~QueueMemcpy(void) { delete[] buf_; }
20 
21 char QueueMemcpy::flush(void) {
22  cnt_ = 0;
23  head_ = 0;
24  tail_ = 0;
25  memset(buf_, 0, annode_size_ * total_);
26 
27  return 0;
28 }
29 
30 long QueueMemcpy::size(void) { return cnt_; }
31 
33  if (total_ <= cnt_)
34  return 1;
35  else
36  return 0;
37 }
38 
39 long QueueMemcpy::node_size(void) { return annode_size_; }
40 
41 char QueueMemcpy::pop(void *data) {
42  std::lock_guard<std::mutex> locker(mutex_);
43  if (0 >= cnt_) {
44  return -1;
45  }
46  if (total_ <= tail_) tail_ = 0;
47 
48  memcpy(data, &buf_[tail_ * annode_size_], annode_size_);
49  tail_++;
50  cnt_--;
51  return 0;
52 }
53 
54 char QueueMemcpy::get(void *data) {
55  std::lock_guard<std::mutex> locker(mutex_);
56  if (0 >= cnt_) {
57  return -1;
58  }
59  if (total_ <= tail_) tail_ = 0;
60 
61  memcpy(data, &buf_[tail_ * annode_size_], annode_size_);
62 
63  return 0;
64 }
65 
66 char QueueMemcpy::push(void *data) {
67  std::lock_guard<std::mutex> locker(mutex_);
68  if (total_ <= cnt_) {
69  return -1;
70  }
71  if (total_ <= head_) head_ = 0;
72 
73  memcpy(&buf_[head_ * annode_size_], data, annode_size_);
74  head_++;
75  cnt_++;
76  return 0;
77 }
QueueMemcpy(long n, long n_size)
Definition: queue_memcpy.cc:12
char pop(void *data)
Definition: queue_memcpy.cc:41
~QueueMemcpy(void)
Definition: queue_memcpy.cc:19
std::mutex mutex_
Definition: queue_memcpy.h:34
char get(void *data)
Definition: queue_memcpy.cc:54
char * buf_
Definition: queue_memcpy.h:32
long size(void)
Definition: queue_memcpy.cc:30
long annode_size_
Definition: queue_memcpy.h:27
char push(void *data)
Definition: queue_memcpy.cc:66
long node_size(void)
Definition: queue_memcpy.cc:39
int is_full(void)
Definition: queue_memcpy.cc:32
char flush(void)
Definition: queue_memcpy.cc:21


xarm_api
Author(s):
autogenerated on Sat May 8 2021 02:51:23