labcomm2006_dynamic_buffer_writer.c 4.05 KB
Newer Older
1
/*
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
2
  labcomm2006_dynamic_buffer_writer.c -- LabComm dynamic memory writer.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

  Copyright 2006-2013 Anders Blomdell <anders.blomdell@control.lth.se>

  This file is part of LabComm.

  LabComm is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  LabComm 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
25
26
27
28
29
30
#include "labcomm2006.h"
#include "labcomm2006_private.h"
#include "labcomm2006_ioctl.h"
#include "labcomm2006_dynamic_buffer_writer.h"

static int dyn_alloc(struct labcomm2006_writer *w, 
31
		     struct labcomm2006_writer_action_context *action_context)
32
33
34
{
  w->data_size = 1000;
  w->count = w->data_size;
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
35
  w->data = labcomm2006_memory_alloc(w->memory, 1, w->data_size);
36
37
38
39
40
41
42
43
  if (w->data == NULL) {
    w->error = -ENOMEM;
  }
  w->pos = 0;

  return w->error;
}

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
44
45
static int dyn_free(struct labcomm2006_writer *w, 
		    struct labcomm2006_writer_action_context *action_context)
46
{
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
47
  labcomm2006_memory_free(w->memory, 1, w->data);
48
49
50
51
  w->data = 0;
  w->data_size = 0;
  w->count = 0;
  w->pos = 0;
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
52
  labcomm2006_memory_free(w->memory, 0, action_context->context);
53
54
55
  return 0;
}

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
56
57
static int dyn_start(struct labcomm2006_writer *w, 
		     struct labcomm2006_writer_action_context *action_context,
58
		     int index,
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
59
		     struct labcomm2006_signature *signature,
60
61
62
63
64
65
		     void *value)
{
  void *tmp;

  w->data_size = 1000;
  w->count = w->data_size;
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
66
  tmp = labcomm2006_memory_realloc(w->memory, 1, w->data, w->data_size);
67
68
69
70
71
72
73
74
75
76
77
  if (tmp != NULL) {
    w->data = tmp;
    w->error = 0;
  } else {
    w->error = -ENOMEM;
  }
  w->pos = 0;

  return w->error;
}

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
78
79
static int dyn_end(struct labcomm2006_writer *w, 
		   struct labcomm2006_writer_action_context *action_context)
80
81
82
83
{
  return 0;
}

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
84
85
static int dyn_flush(struct labcomm2006_writer *w, 
		     struct labcomm2006_writer_action_context *action_context)
86
87
88
89
90
{
  void *tmp;

  w->data_size += 1000;
  w->count = w->data_size;
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
91
  tmp = labcomm2006_memory_realloc(w->memory, 1, w->data, w->data_size);
92
93
94
95
96
97
98
99
100
101
102
  if (tmp != NULL) {
    w->data = tmp;
    w->error = 0;
  } else {
    /* Old pointer in w->data still valid */
    w->error = -ENOMEM;
  }

  return w->error; 
}

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
103
104
static int dyn_ioctl(struct labcomm2006_writer *w, 
		     struct labcomm2006_writer_action_context *action_context, 
105
		     int signature_index,
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
106
		     struct labcomm2006_signature *signature,
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
		     uint32_t action, va_list arg)
{
  int result = -ENOTSUP;
  switch (action) {
    case LABCOMM_IOCTL_WRITER_GET_BYTES_WRITTEN: {
      int *value = va_arg(arg, int*);
      *value = w->pos;
      result = 0;
    } break;
    case LABCOMM_IOCTL_WRITER_GET_BYTE_POINTER: {
      void **value = va_arg(arg, void**);
      *value = w->data;
      result = 0;
    } break;
  }
  return result;
}

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
125
static const struct labcomm2006_writer_action action = {
126
127
128
129
130
131
132
  .alloc = dyn_alloc,
  .free = dyn_free,
  .start = dyn_start,
  .end = dyn_end,
  .flush = dyn_flush,
  .ioctl = dyn_ioctl
};
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
133
const struct labcomm2006_writer_action *labcomm2006_dynamic_buffer_writer_action = 
134
135
  &action;

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
136
137
struct labcomm2006_writer *labcomm2006_dynamic_buffer_writer_new(
  struct labcomm2006_memory *memory)
138
139
{
  struct result {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
140
141
    struct labcomm2006_writer writer;
    struct labcomm2006_writer_action_context action_context;
142
143
  } *result;

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
144
  result = labcomm2006_memory_alloc(memory, 0, sizeof(*result));
145
146
147
148
149
150
151
152
153
154
155
  if (result != NULL) {
    result->action_context.next = NULL;
    result->action_context.context = result;
    result->action_context.action = &action;
    result->writer.action_context = &result->action_context;
    result->writer.memory = memory;
    return &result->writer;
  }
  return NULL;
}