Blame view

RIOT/sys/pipe/pipe_dynamic.c 1.56 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  /*
   * Copyright (C) 2014  René Kijewski  <rene.kijewski@fu-berlin.de>
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   */
  
  /**
   * @ingroup     sys_pipe
   * @{
   * @file
   * @brief       Implementation for dynamically allocated pipes.
   * @author      René Kijewski <rene.kijewski@fu-berlin.de>
   * @}
   */
  
  #if defined(MCU_ATMEGA2560) || defined(MCU_ATMEGA1281) || defined(MCU_ATMEGA328P)
  #include <stdlib.h>
  #else
  #include <malloc.h>
  #endif
  
  #include "pipe.h"
  
  struct mallocd_pipe
  {
      pipe_t pipe;
      ringbuffer_t rb;
      char buffer[1];
  };
  
  pipe_t *pipe_malloc(unsigned size)
  {
      struct mallocd_pipe *m_pipe = malloc(sizeof (*m_pipe) + size);
      if (m_pipe) {
          ringbuffer_init(&m_pipe->rb, m_pipe->buffer, size);
          pipe_init(&m_pipe->pipe, &m_pipe->rb, free);
      }
      return &m_pipe->pipe;
  }
  
  void pipe_free(pipe_t *rp)
  {
      if (rp && rp->free) {
          rp->free(rp);
      }
  }