Advertisement
Guest User

Tuple.c

a guest
Jun 26th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. /*
  2.     C-Tuple - Tuple style generic types in C.
  3.     Copyright (c) 2016 Tristen Horton
  4.  
  5.     This program is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (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, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdarg.h>
  19. #include <stdlib.h>
  20. #include "tuple.h"
  21.  
  22. Tuple* TUPLECALL CreateTuple(int n_args, ...) {
  23.     va_list varlist;
  24.     va_start(varlist, n_args);
  25.  
  26.     int varsSize;
  27.     void** vars = (void**)malloc(n_args * sizeof(void*));
  28.  
  29.     for (int i = 0; i < n_args; i++) {
  30.         void* arg = va_arg(varlist, void*);
  31.  
  32.         varsSize += sizeof(arg);
  33.  
  34.         vars[i] = arg;
  35.     }
  36.  
  37.     // Size of all of the arguments + size of an int value (varsSize) since Tuple has an array of void* and a single int.
  38.     Tuple* t = (Tuple*)malloc(varsSize + sizeof(varsSize));
  39.  
  40.     t->values = vars;
  41.     t->tSize = n_args;
  42.  
  43.     va_end(varlist);
  44.  
  45.     return t;
  46. }
  47.  
  48. void TUPLECALL DestroyTuple(Tuple* t) {
  49.     free(t->values);
  50.     free(t);
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement