photon  1.1
photon_buffer_internal.c
Go to the documentation of this file.
1 // =============================================================================
2 // Photon RDMA Library (libphoton)
3 //
4 // Copyright (c) 2016, Trustees of Indiana University,
5 // All rights reserved.
6 //
7 // This software may be modified and distributed under the terms of the BSD
8 // license. See the COPYING file for details.
9 //
10 // This software was created at the Indiana University Center for Research in
11 // Extreme Scale Technologies (CREST).
12 // =============================================================================
13 
14 #include <stdlib.h>
15 #include <string.h>
16 #include <inttypes.h>
17 
18 #include "libphoton.h"
19 #include "photon_backend.h"
20 #include "photon_buffer_internal.h"
21 
22 static photonBufferInterface ifaces[BUFFER_IFACES_MAX];
23 static int iface_count = 0;
24 
25 int photon_buffer_init(photonBufferInterface bi) {
26  if (iface_count >= BUFFER_IFACES_MAX) {
27  log_err("Exceeded max buffer interfaces: %d", BUFFER_IFACES_MAX);
28  return PHOTON_ERROR;
29  }
30  ifaces[iface_count++] = bi;
31  return PHOTON_OK;
32 }
33 
34 photonBufferHandle _photon_buffer_create(void *addr, uint64_t size, int flags) {
35  photonBufferHandle new_buf;
36 
37  dbg_trace("%llu", size);
38 
39  new_buf = malloc(sizeof(struct photon_buffer_handle_t));
40  if (!new_buf) {
41  log_err("malloc failed");
42  return NULL;
43  }
44 
45  memset(new_buf, 0, sizeof(*new_buf));
46 
47  new_buf->bint.buf.addr = (uintptr_t)addr;
48  new_buf->bint.buf.size = size;
49  new_buf->bint.buf.priv = (struct photon_buffer_priv_t){0,0};
50  new_buf->bint.priv_ptr = NULL;
51  new_buf->bint.priv_size = 0;
52 
53  // TODO: setup only the appropriate buffer interfaces for this handle
54  new_buf->ifaces = ifaces;
55  new_buf->iface_count = iface_count;
56 
57  new_buf->ref_count = 1;
58  new_buf->is_registered = 0;
59 
60  dbg_trace("allocated buffer handle: %p", new_buf);
61  dbg_trace("contains ptr to addr: %p of size %" PRIu64, addr, size);
62 
63  return new_buf;
64 }
65 
66 photonBufferHandle photon_buffer_create(void *addr, uint64_t size, int flags) {
67  int i;
68 
69  if (iface_count <= 0) {
70  log_err("No buffer interfaces set!");
71  return NULL;
72  }
73 
74  photonBufferHandle buf = _photon_buffer_create(addr, size, flags);
75  if (buf) {
76  for (i=0; i<buf->iface_count; i++) {
77  if (buf->ifaces[i]->buffer_create)
78  buf->ifaces[i]->buffer_create(&(buf->bint), flags);
79  }
80  }
81 
82  return buf;
83 }
84 
86  int i;
87 
88  if (iface_count <= 0) {
89  log_err("No buffer interfaces set!");
90  return;
91  }
92 
93  for (i=0; i<buf->iface_count; i++) {
94  if (buf->ifaces[i]->buffer_free)
95  buf->ifaces[i]->buffer_free(&(buf->bint));
96  }
97 }
98 
100  int i, err = PHOTON_OK;
101 
102  if (iface_count <= 0) {
103  log_err("No buffer interfaces set!");
104  return PHOTON_ERROR;
105  }
106 
107  for (i=0; i<buf->iface_count; i++) {
108  if (buf->ifaces[i]->buffer_register)
109  err += buf->ifaces[i]->buffer_register(&(buf->bint), flags);
110  }
111 
112  if (err == PHOTON_OK) {
113  buf->is_registered = 1;
114  }
115 
116  return err;
117 }
118 
120  int i, err = PHOTON_OK;
121 
122  if (iface_count <= 0) {
123  log_err("No buffer interfaces set!");
124  return PHOTON_ERROR;
125  }
126 
127  for (i=0; i<buf->iface_count; i++) {
128  if (buf->ifaces[i]->buffer_unregister)
129  err += buf->ifaces[i]->buffer_unregister(&(buf->bint));
130  }
131 
132  if (err == PHOTON_OK) {
133  buf->is_registered = 0;
134  }
135 
136  return err;
137 }
photonBufferHandle photon_buffer_create(void *addr, uint64_t size, int flags)
#define BUFFER_IFACES_MAX
struct photon_buffer_handle_t * photonBufferHandle
photonBufferHandle _photon_buffer_create(void *addr, uint64_t size, int flags)
void photon_buffer_free(photonBufferHandle buf)
int photon_buffer_register(photonBufferHandle buf, int flags)
#define PHOTON_ERROR
Error code, general error.
Definition: photon.h:32
int photon_buffer_unregister(photonBufferHandle buf)
#define PHOTON_OK
Photon success code.
Definition: photon.h:30
int photon_buffer_init(photonBufferInterface bi)
Convenience pointer type for the private buffer structure.
Definition: photon.h:98