|
gimme-alpha 0.1
|
00001 /* 00002 ** nanostructures.c 00003 ** 00004 ** Made by (Johnny Q. Hacker) 00005 ** Login <solarion@borkborkbork> 00006 00007 Copyright (C) 2009 Joseph Pingenot 00008 00009 This program is free software: you can redistribute it and/or modify 00010 it under the terms of the GNU Affero General Public License as published by 00011 the Free Software Foundation, either version 3 of the License, or 00012 (at your option) any later version. 00013 00014 This program is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU Affero General Public License for more details. 00018 00019 You should have received a copy of the GNU Affero General Public License 00020 along with this program. If not, see <http://www.gnu.org/licenses/>. 00021 00022 ** Started on Fri Mar 20 16:38:26 2009 Johnny Q. Hacker 00023 ** Last update Sun May 12 01:17:25 2002 Speed Blue 00024 */ 00025 00026 #include <unistd.h> 00027 #include <stdlib.h> 00028 #include <libmodeling/nanostructures.h> 00029 #include <libcommon/gcc_hints.h> 00030 00031 00032 /*==================================================================== 00033 FREES 00034 ====================================================================*/ 00035 00036 static void region_1d_free(struct region_1d *region) { 00037 free(region); 00038 } 00039 00040 static void doping_region_1d_free(struct doping_region_1d *region) { 00041 free(region); 00042 } 00043 00044 00045 static void foreach_region_1d_free(gpointer region, gpointer private) { 00046 region_1d_free(region); 00047 } 00048 00049 static void foreach_doping_region_1d_free(gpointer region, gpointer private) { 00050 doping_region_1d_free(region); 00051 } 00052 00053 void nanostructure_1d_free(struct nanostructure_1d* s) { 00054 g_list_foreach(s->regions, foreach_region_1d_free, NULL); 00055 g_list_foreach(s->doping_regions, foreach_doping_region_1d_free, NULL); 00056 free(s); 00057 } 00058 00059 void nanostructure_free(struct nanostructure* s) { 00060 if(likely((s->storage) != NULL)) { 00061 switch(s->type) { 00062 case NS_D1: 00063 nanostructure_1d_free(s->storage); 00064 break; 00065 default: 00066 nanostructure_free(s); 00067 } 00068 } 00069 free(s); 00070 } 00071 00072 /*==================================================================== 00073 NEWS 00074 ====================================================================*/ 00075 00076 struct doping_region_1d* doping_region_1d_new(enum doping_type dt) { 00077 struct doping_region_1d *dr; 00078 if(unlikely((dr = malloc(sizeof(struct doping_region_1d))) == NULL)) return NULL; 00079 return dr; 00080 } 00081 00082 struct region_1d* region_1d_new() { 00083 struct region_1d *r; 00084 if(unlikely((r = malloc(sizeof(struct region_1d))) == NULL)) return NULL; 00085 return r; 00086 } 00087 00088 struct nanostructure_1d* nanostructure_1d_new() { 00089 struct nanostructure_1d *s; 00090 if(unlikely((s = malloc(sizeof(struct nanostructure_1d))) == NULL)) return NULL; 00091 s->regions = s->doping_regions = NULL; 00092 return s; 00093 } 00094 00095 struct nanostructure* nanostructure_new(enum nanostructure_type type) { 00096 struct nanostructure *s; 00097 if(unlikely((s = malloc(sizeof(struct nanostructure))) == NULL)) return NULL; 00098 switch(type) { 00099 case NS_D1: 00100 s->type = type; 00101 s->storage = nanostructure_1d_new(); 00102 break; 00103 default: 00104 s->storage = NULL; 00105 } 00106 /*Catch allocation errors and de-allocate if we errored*/ 00107 if(unlikely((s->storage) == NULL)) { 00108 nanostructure_free(s); 00109 } 00110 return s; 00111 }