PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
mbuf.h
Go to the documentation of this file.
1 /*
2  * mbuf.h
3  * Memory buffer operations.
4  *
5  * Copyright (c) 2005 Marko Kreen
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * contrib/pgcrypto/mbuf.h
30  */
31 
32 #ifndef __PX_MBUF_H
33 #define __PX_MBUF_H
34 
35 typedef struct MBuf MBuf;
36 typedef struct PushFilter PushFilter;
37 typedef struct PullFilter PullFilter;
40 
42 {
43  /*
44  * should return needed buffer size, 0- no buffering, <0 on error if NULL,
45  * no buffering, and priv=init_arg
46  */
47  int (*init) (PushFilter *next, void *init_arg, void **priv_p);
48 
49  /*
50  * send data to next. should consume all? if null, it will be simply
51  * copied (in-place) returns 0 on error
52  */
53  int (*push) (PushFilter *next, void *priv,
54  const uint8 *src, int len);
55  int (*flush) (PushFilter *next, void *priv);
56  void (*free) (void *priv);
57 };
58 
60 {
61  /*
62  * should return needed buffer size, 0- no buffering, <0 on error if NULL,
63  * no buffering, and priv=init_arg
64  */
65  int (*init) (void **priv_p, void *init_arg, PullFilter *src);
66 
67  /*
68  * request data from src, put result ptr to data_p can use ptr from src or
69  * use buf as work area if NULL in-place copy
70  */
71  int (*pull) (void *priv, PullFilter *src, int len,
72  uint8 **data_p, uint8 *buf, int buflen);
73  void (*free) (void *priv);
74 };
75 
76 /*
77  * Memory buffer
78  */
79 MBuf *mbuf_create(int len);
80 MBuf *mbuf_create_from_data(uint8 *data, int len);
81 int mbuf_tell(MBuf *mbuf);
82 int mbuf_avail(MBuf *mbuf);
83 int mbuf_size(MBuf *mbuf);
84 int mbuf_grab(MBuf *mbuf, int len, uint8 **data_p);
85 int mbuf_steal_data(MBuf *mbuf, uint8 **data_p);
86 int mbuf_append(MBuf *dst, const uint8 *buf, int cnt);
87 int mbuf_rewind(MBuf *mbuf);
88 int mbuf_free(MBuf *mbuf);
89 
90 /*
91  * Push filter
92  */
93 int pushf_create(PushFilter **res, const PushFilterOps *ops, void *init_arg,
94  PushFilter *next);
95 int pushf_write(PushFilter *mp, const uint8 *data, int len);
96 void pushf_free_all(PushFilter *mp);
97 void pushf_free(PushFilter *mp);
98 int pushf_flush(PushFilter *mp);
99 
100 int pushf_create_mbuf_writer(PushFilter **mp_p, MBuf *mbuf);
101 
102 /*
103  * Pull filter
104  */
105 int pullf_create(PullFilter **res, const PullFilterOps *ops,
106  void *init_arg, PullFilter *src);
107 int pullf_read(PullFilter *mp, int len, uint8 **data_p);
108 int pullf_read_max(PullFilter *mp, int len,
109  uint8 **data_p, uint8 *tmpbuf);
110 void pullf_free(PullFilter *mp);
111 int pullf_read_fixed(PullFilter *src, int len, uint8 *dst);
112 
113 int pullf_create_mbuf_reader(PullFilter **pf_p, MBuf *mbuf);
114 
115 #define GETBYTE(pf, dst) \
116  do { \
117  uint8 __b; \
118  int __res = pullf_read_fixed(pf, 1, &__b); \
119  if (__res < 0) \
120  return __res; \
121  (dst) = __b; \
122  } while (0)
123 
124 #endif /* __PX_MBUF_H */
MBuf * mbuf_create_from_data(uint8 *data, int len)
Definition: mbuf.c:139
static int32 next
Definition: blutils.c:210
void(* free)(void *priv)
Definition: mbuf.h:73
unsigned char uint8
Definition: c.h:263
int(* init)(PushFilter *next, void *init_arg, void **priv_p)
Definition: mbuf.h:47
int pushf_flush(PushFilter *mp)
Definition: mbuf.c:516
int mbuf_rewind(MBuf *mbuf)
Definition: mbuf.c:170
int mbuf_size(MBuf *mbuf)
Definition: mbuf.c:56
MBuf * mbuf_create(int len)
Definition: mbuf.c:119
static char * buf
Definition: pg_test_fsync.c:65
int pullf_read_max(PullFilter *mp, int len, uint8 **data_p, uint8 *tmpbuf)
Definition: mbuf.c:279
int pullf_create_mbuf_reader(PullFilter **pf_p, MBuf *mbuf)
Definition: mbuf.c:352
int pullf_read(PullFilter *mp, int len, uint8 **data_p)
Definition: mbuf.c:262
void pushf_free(PushFilter *mp)
Definition: mbuf.c:412
int(* push)(PushFilter *next, void *priv, const uint8 *src, int len)
Definition: mbuf.h:53
void(* free)(void *priv)
Definition: mbuf.h:56
int(* flush)(PushFilter *next, void *priv)
Definition: mbuf.h:55
int(* pull)(void *priv, PullFilter *src, int len, uint8 **data_p, uint8 *buf, int buflen)
Definition: mbuf.h:71
int pushf_create(PushFilter **res, const PushFilterOps *ops, void *init_arg, PushFilter *next)
Definition: mbuf.c:373
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
int(* init)(void **priv_p, void *init_arg, PullFilter *src)
Definition: mbuf.h:65
int mbuf_steal_data(MBuf *mbuf, uint8 **data_p)
Definition: mbuf.c:177
int pullf_read_fixed(PullFilter *src, int len, uint8 *dst)
Definition: mbuf.c:317
Definition: mbuf.c:39
int pullf_create(PullFilter **res, const PullFilterOps *ops, void *init_arg, PullFilter *src)
Definition: mbuf.c:206
void pushf_free_all(PushFilter *mp)
Definition: mbuf.c:428
static StringInfoData tmpbuf
Definition: walsender.c:153
int mbuf_avail(MBuf *mbuf)
Definition: mbuf.c:50
int mbuf_free(MBuf *mbuf)
Definition: mbuf.c:68
int mbuf_grab(MBuf *mbuf, int len, uint8 **data_p)
Definition: mbuf.c:157
int mbuf_append(MBuf *dst, const uint8 *buf, int cnt)
Definition: mbuf.c:102
int mbuf_tell(MBuf *mbuf)
Definition: mbuf.c:62
void pullf_free(PullFilter *mp)
Definition: mbuf.c:245
int pushf_create_mbuf_writer(PushFilter **mp_p, MBuf *mbuf)
Definition: mbuf.c:561