kinetic-c  v0.12.0
Seagate Kinetic Protocol Client Library for C
kinetic_allocator.c
Go to the documentation of this file.
1 /*
2 * kinetic-c
3 * Copyright (C) 2015 Seagate Technology.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20 
21 #include "kinetic_allocator.h"
22 #include "kinetic_logger.h"
23 #include "kinetic_memory.h"
24 #include "kinetic_resourcewaiter.h"
26 #include <stdlib.h>
27 #include <pthread.h>
28 
29 
30 KineticSession* KineticAllocator_NewSession(struct bus * b, KineticSessionConfig* config)
31 {
32  (void)b; // TODO: combine session w/connection, which will use this variable
33 
34  // Allocate a new session
35  KineticSession* session = KineticCalloc(1, sizeof(KineticSession));
36  if (session == NULL) {
37  LOG0("Failed allocating a new session!");
38  return NULL;
39  }
40 
41  // Deep copy the supplied config internally
42  session->config = *config;
43  memcpy(session->config.keyData, config->hmacKey.data, config->hmacKey.len);
44  session->config.hmacKey.data = session->config.keyData;
45  strncpy(session->config.host, config->host, sizeof(session->config.host));
46  session->timeoutSeconds = config->timeoutSeconds; // TODO: Eliminate this, since already in config?
47  KineticResourceWaiter_Init(&session->connectionReady);
48  session->messageBus = b;
49  session->socket = KINETIC_SOCKET_INVALID; // start with an invalid file descriptor
50  session->terminationStatus = KINETIC_STATUS_SUCCESS;
51  return session;
52 }
53 
54 void KineticAllocator_FreeSession(KineticSession* session)
55 {
56  if (session != NULL) {
57  KineticResourceWaiter_Destroy(&session->connectionReady);
58  KineticFree(session);
59  }
60 }
61 
63 {
64  KineticResponse * response = KineticCalloc(1, sizeof(*response) + valueLength);
65  if (response == NULL) {
66  LOG0("Failed allocating new response!");
67  return NULL;
68  }
69  return response;
70 }
71 
73 {
74  KINETIC_ASSERT(response != NULL);
75 
76  if (response->command != NULL) {
77  protobuf_c_message_free_unpacked(&response->command->base, NULL);
78  }
79  if (response->proto != NULL) {
80  protobuf_c_message_free_unpacked(&response->proto->base, NULL);
81  }
82  KineticFree(response);
83 }
84 
85 KineticOperation* KineticAllocator_NewOperation(KineticSession* const session)
86 {
87  KINETIC_ASSERT(session != NULL);
88 
89  LOGF3("Allocating new operation on session %p", (void*)session);
90  KineticOperation* newOperation =
91  (KineticOperation*)KineticCalloc(1, sizeof(KineticOperation));
92  if (newOperation == NULL) {
93  LOGF0("Failed allocating new operation on session %p", (void*)session);
94  return NULL;
95  }
96  newOperation->session = session;
97  newOperation->timeoutSeconds = session->timeoutSeconds; // TODO: use timeout in config throughput
98  newOperation->request = (KineticRequest*)KineticCalloc(1, sizeof(KineticRequest));
99  if (newOperation->request == NULL) {
100  LOGF0("Failed allocating new PDU on session %p", (void*)session);
101  KineticFree(newOperation);
102  return NULL;
103  }
104  KineticRequest_Init(newOperation->request, session);
105  return newOperation;
106 }
107 
108 void KineticAllocator_FreeOperation(KineticOperation* operation)
109 {
110  KINETIC_ASSERT(operation != NULL);
111  LOGF3("Freeing operation %p on session %p", (void*)operation, (void*)operation->session);
112  if (operation->request != NULL) {
113  KineticFree(operation->request);
114  operation->request = NULL;
115  }
116  if (operation->response != NULL) {
117  KineticAllocator_FreeKineticResponse(operation->response);
118  operation->response = NULL;
119  }
120  KineticFree(operation);
121 }
122 
123 void KineticAllocator_FreeP2PProtobuf(Com__Seagate__Kinetic__Proto__Command__P2POperation* proto_p2pOp)
124 {
125  if (proto_p2pOp != NULL) {
126  if (proto_p2pOp->peer != NULL) {
127  free(proto_p2pOp->peer);
128  proto_p2pOp->peer = NULL;
129  }
130  if (proto_p2pOp->operation != NULL) {
131  for(size_t i = 0; i < proto_p2pOp->n_operation; i++) {
132  if (proto_p2pOp->operation[i] != NULL) {
133  if (proto_p2pOp->operation[i]->p2pop != NULL) {
134  KineticAllocator_FreeP2PProtobuf(proto_p2pOp->operation[i]->p2pop);
135  proto_p2pOp->operation[i]->p2pop = NULL;
136  }
137  if (proto_p2pOp->operation[i]->status != NULL) {
138  free(proto_p2pOp->operation[i]->status);
139  proto_p2pOp->operation[i]->status = NULL;
140  }
141  free(proto_p2pOp->operation[i]);
142  proto_p2pOp->operation[i] = NULL;
143  }
144  }
145  free(proto_p2pOp->operation);
146  proto_p2pOp->operation = NULL;
147  }
148  free(proto_p2pOp);
149  }
150 }
void * KineticCalloc(size_t count, size_t size)
Operation successful.
#define KINETIC_SOCKET_INVALID
Invalid socket file descriptor value.
Definition: kinetic_types.h:39
Structure used to specify the configuration for a session.
void KineticAllocator_FreeOperation(KineticOperation *operation)
Com__Seagate__Kinetic__Proto__Message * proto
void KineticAllocator_FreeSession(KineticSession *session)
KineticResponse * KineticAllocator_NewKineticResponse(size_t const valueLength)
Message bus.
uint16_t timeoutSeconds
Operation timeout. If 0, use the default (10 seconds).
char host[256]
Host name/IP address of Kinetic Device.
void KineticAllocator_FreeP2PProtobuf(Com__Seagate__Kinetic__Proto__Command__P2POperation *proto_p2pOp)
void KineticRequest_Init(KineticRequest *request, KineticSession const *const session)
#define KINETIC_ASSERT(cond)
void KineticFree(void *pointer)
#define LOG0(message)
Com__Seagate__Kinetic__Proto__Command * command
#define LOGF3(message,...)
size_t len
Number of bytes in the data field.
Definition: byte_array.h:35
void KineticResourceWaiter_Init(KineticResourceWaiter *const waiter)
#define LOGF0(message,...)
KineticOperation * KineticAllocator_NewOperation(KineticSession *const session)
void KineticAllocator_FreeKineticResponse(KineticResponse *response)
uint8_t * data
Pointer to an allocated array of data bytes.
Definition: byte_array.h:36
KineticSession * KineticAllocator_NewSession(struct bus *b, KineticSessionConfig *config)
void KineticResourceWaiter_Destroy(KineticResourceWaiter *const waiter)