Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
uniform allocation is better
[simgrid.git] / src / smpi / mpi / smpi_info.cpp
1 /* Copyright (c) 2007-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "smpi_info.hpp"
8 #include "xbt/ex.hpp"
9 #include "xbt/sysdep.h"
10
11 namespace simgrid{
12 namespace smpi{
13
14 Info::Info():refcount_(1){
15   dict_= xbt_dict_new_homogeneous(xbt_free_f);
16 }
17
18 Info::Info(Info* info):refcount_(1){
19   dict_= xbt_dict_new_homogeneous(xbt_free_f);
20   xbt_dict_cursor_t cursor = nullptr;
21   char* key;
22   void* data;
23   xbt_dict_foreach(info->dict_,cursor,key,data){
24     xbt_dict_set(dict_, key, xbt_strdup(static_cast<char*>(data)), nullptr);
25   }
26 }
27
28 Info::~Info(){
29   xbt_dict_free(&dict_);
30 }
31
32 void Info::ref(){
33   refcount_++;
34 }
35
36 void Info::unref(Info* info){
37   info->refcount_--;
38   if(info->refcount_==0){
39     delete info;
40   }
41 }
42
43 void Info::set(char *key, char *value){
44   xbt_dict_set(dict_, key, xbt_strdup(value), nullptr);
45 }
46
47 int Info::get(char *key, int valuelen, char *value, int *flag){
48   *flag=false;
49   char* tmpvalue=static_cast<char*>(xbt_dict_get_or_null(dict_, key));
50   if(tmpvalue){
51     memset(value, 0, valuelen);
52     memcpy(value,tmpvalue, (strlen(tmpvalue) + 1 < static_cast<size_t>(valuelen)) ? strlen(tmpvalue) + 1 : valuelen);
53     *flag=true;
54   }
55   return MPI_SUCCESS;
56 }
57
58
59 int Info::remove(char *key){
60   try {
61     xbt_dict_remove(dict_, key);
62   }
63   catch(xbt_ex& e){
64     return MPI_ERR_INFO_NOKEY;
65   }
66   return MPI_SUCCESS;
67 }
68
69 int Info::get_nkeys(int *nkeys){
70   *nkeys=xbt_dict_size(dict_);
71   return MPI_SUCCESS;
72 }
73
74 int Info::get_nthkey(int n, char *key){
75   xbt_dict_cursor_t cursor = nullptr;
76   char *keyn;
77   void* data;
78   int num=0;
79   xbt_dict_foreach(dict_,cursor,keyn,data){
80     if(num==n){
81       strncpy(key,keyn,strlen(keyn)+1);
82       xbt_dict_cursor_free(&cursor);
83       return MPI_SUCCESS;
84     }
85     num++;
86   }
87   return MPI_ERR_ARG;
88 }
89
90 int Info::get_valuelen(char *key, int *valuelen, int *flag){
91   *flag=false;
92   char* tmpvalue=static_cast<char*>(xbt_dict_get_or_null(dict_, key));
93   if(tmpvalue){
94     *valuelen=strlen(tmpvalue);
95     *flag=true;
96   }
97   return MPI_SUCCESS;
98 }
99
100 Info* Info::f2c(int id){
101   return static_cast<Info*>(F2C::f2c(id));
102 }
103
104 }
105 }
106