GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
url-handle-manager.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2017-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 // Extracted from urlwrite.cc.
24 
25 #if defined (HAVE_CONFIG_H)
26 # include "config.h"
27 #endif
28 
29 #include <cmath>
30 
31 #include "url-handle-manager.h"
32 
33 static double
35 {
36  static double maxrand = RAND_MAX + 2.0;
37 
38  return (rand () + 1.0) / maxrand;
39 }
40 
41 namespace octave
42 {
44  {
46 
47  // Curl handles are negative integers plus some random fractional
48  // part. To avoid running out of integers, we recycle the integer
49  // part but tack on a new random part each time.
50 
52 
53  if (p != handle_free_list.end ())
54  {
55  retval = *p;
56  handle_free_list.erase (p);
57  }
58  else
59  {
61 
63  }
64 
65  return retval;
66  }
67 
69  {
70  if (h.ok ())
71  {
72  iterator p = handle_map.find (h);
73 
74  if (p == handle_map.end ())
75  error ("url_handle_manager::free: invalid object %g", h.value ());
76 
77  // Curl handles are negative integers plus some random
78  // fractional part. To avoid running out of integers, we
79  // recycle the integer part but tack on a new random part
80  // each time.
81 
82  handle_map.erase (p);
83 
84  if (h.value () < 0)
85  handle_free_list.insert
86  (std::ceil (h.value ()) - make_handle_fraction ());
87  }
88  }
89 }
std::set< url_handle >::iterator free_list_iterator
octave_handle url_handle
std::set< url_handle > handle_free_list
std::map< url_handle, url_transfer > handle_map
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:112
void error(const char *fmt,...)
Definition: error.cc:578
std::map< url_handle, url_transfer >::iterator iterator
double h
Definition: graphics.cc:11808
void free(const url_handle &h)
octave_value retval
Definition: data.cc:6246
static double make_handle_fraction(void)
p
Definition: lu.cc:138