NCBI C++ ToolKit
ncbi_std.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ncbi_std.c 72378 2016-05-04 14:59:01Z camacho $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  */
27 
28 /** @file ncbi_std.c
29  * A few utilities needed by code in algo/blast/core but not provided elsewhere.
30  */
31 
32 #include <algo/blast/core/blast_def.h> /* for sfree() macro */
34 
35 void * BlastMemDup (const void *orig, size_t size)
36 {
37  void* copy;
38 
39  if (orig == NULL || size == 0)
40  return NULL;
41 
42  if ((copy = malloc (size)) == NULL)
43  return NULL;
44 
45  memcpy(copy, orig, size);
46  return copy;
47 }
48 
49 /*****************************************************************************
50 *
51 * ListNodeNew(vnp)
52 * adds after last node in list if vnp not NULL
53 *
54 *****************************************************************************/
56 {
57  ListNode* newnode;
58 
59  newnode = (ListNode*) calloc(1, sizeof(ListNode));
60  if (vnp != NULL)
61  {
62  while (vnp->next != NULL)
63  vnp = vnp->next;
64  vnp->next = newnode;
65  }
66  return newnode;
67 }
68 
69 /*****************************************************************************
70 *
71 * ListNodeAdd(head)
72 * adds after last node in list if *head not NULL
73 * If *head is NULL, sets it to the new ListNode
74 * returns pointer to the NEW node added
75 *
76 *****************************************************************************/
78 {
79  ListNode* newnode;
80 
81  if (head != NULL)
82  {
83  newnode = ListNodeNew(*head);
84  if (*head == NULL)
85  *head = newnode;
86  }
87  else
88  newnode = ListNodeNew(NULL);
89 
90  return newnode;
91 }
92 
93 /* ListNodeAddPointer (head, choice, value)
94 * adds like ListNodeAdd()
95 * sets newnode->choice = choice (if choice does not matter, use 0)
96 * sets newnode->ptr = value
97 *
98 *****************************************************************************/
100  void *value)
101 {
102  ListNode* newnode;
103 
104  newnode = ListNodeAdd(head);
105  if (newnode != NULL)
106  {
107  newnode->choice = choice;
108  newnode->ptr = value;
109  }
110 
111  return newnode;
112 }
113 
114 /*****************************************************************************
115 *
116 * ListNodeCopyStr (head, choice, str)
117 * adds like ListNodeAdd()
118 * sets newnode->choice = choice (if choice does not matter, use 0)
119 * sets newnode->ptr = str
120 * makes a COPY of str
121 * if str == NULL, does not add a ListNode
122 *
123 *****************************************************************************/
124 ListNode* ListNodeCopyStr (ListNode** head, Uint1 choice, const char* str)
125 {
126  ListNode* newnode;
127 
128  if (str == NULL) return NULL;
129 
130  newnode = ListNodeAdd(head);
131  if (newnode != NULL)
132  {
133  newnode->choice = choice;
134  newnode->ptr = strdup(str);
135  }
136 
137  return newnode;
138 }
139 
140 /*****************************************************************************
141 *
142 * ListNodeFree(vnp)
143 * frees whole chain of ListNodes
144 * Does NOT free associated data pointers
145 * see ListNodeFreeData()
146 *
147 *****************************************************************************/
149 {
150  ListNode* next;
151 
152  while (vnp != NULL)
153  {
154  next = vnp->next;
155  sfree(vnp);
156  vnp = next;
157  }
158  return NULL;
159 }
160 
161 /*****************************************************************************
162 *
163 * ListNodeFreeData(vnp)
164 * frees whole chain of ListNodes
165 * frees associated data pointers - BEWARE of this if these are not
166 * allocated single memory block structures.
167 *
168 *****************************************************************************/
170 {
171  ListNode* next;
172 
173  while (vnp != NULL)
174  {
175  sfree(vnp->ptr);
176  next = vnp->next;
177  sfree(vnp);
178  vnp = next;
179  }
180  return NULL;
181 }
Definitions used throughout BLAST.
#define sfree(x)
Safe free a pointer: belongs to a higher level header.
Definition: blast_def.h:112
#define head
Definition: ct_nlmzip_i.h:138
static DLIST_TYPE *DLIST_NAME() next(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:56
static const char * str(char *buf, int n)
Definition: stats.c:84
#define NULL
Definition: ncbistd.hpp:225
uint8_t Uint1
1-byte (8-bit) unsigned integer
Definition: ncbitype.h:99
where boath are integers</td > n< td ></td > n</tr > n< tr > n< td > tse</td > n< td > optional</td > n< td > String</td > n< td class=\"description\"> TSE option controls what blob is orig
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
#define strdup
Definition: ncbi_ansi_ext.h:70
ListNode * ListNodeNew(ListNode *vnp)
Create a new list node.
Definition: ncbi_std.c:55
ListNode * ListNodeCopyStr(ListNode **head, Uint1 choice, const char *str)
Add a node to the list with a provided choice, and attached data pointing to a provided string.
Definition: ncbi_std.c:124
ListNode * ListNodeAdd(ListNode **head)
Add a node to the list.
Definition: ncbi_std.c:77
void * BlastMemDup(const void *orig, size_t size)
Copies memory using memcpy and malloc.
Definition: ncbi_std.c:35
ListNode * ListNodeAddPointer(ListNode **head, Uint1 choice, void *value)
Add a node to the list with a given choice and data pointer.
Definition: ncbi_std.c:99
ListNode * ListNodeFreeData(ListNode *vnp)
Free nodes as well as data (vnp->ptr) assuming it is one contiguous chunk.
Definition: ncbi_std.c:169
ListNode * ListNodeFree(ListNode *vnp)
Free all list's nodes, does not attempt to free data.
Definition: ncbi_std.c:148
Type and macro definitions from C toolkit that are not defined in C++ toolkit.
void copy(Njn::Matrix< S > *matrix_, const Njn::Matrix< T > &matrix0_)
Definition: njn_matrix.hpp:613
A generic linked list node structure.
Definition: ncbi_std.h:213
void * ptr
attached data
Definition: ncbi_std.h:215
struct ListNode * next
next in linked list
Definition: ncbi_std.h:216
Uint1 choice
to pick a choice
Definition: ncbi_std.h:214
voidp malloc(uInt size)
voidp calloc(uInt items, uInt size)
Modified on Wed Apr 17 13:09:06 2024 by modify_doxy.py rev. 669887