GNU Octave  8.1.0
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-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 // Extracted from urlwrite.cc.
27 
28 #if defined (HAVE_CONFIG_H)
29 # include "config.h"
30 #endif
31 
32 #include <cmath>
33 
34 #include "url-handle-manager.h"
35 
37 
38 static double
40 {
41  static double maxrand = RAND_MAX + 2.0;
42 
43  return (rand () + 1.0) / maxrand;
44 }
45 
47 {
48  url_handle retval;
49 
50  // Curl handles are negative integers plus some random fractional
51  // part. To avoid running out of integers, we recycle the integer
52  // part but tack on a new random part each time.
53 
54  auto p = m_handle_free_list.begin ();
55 
56  if (p != m_handle_free_list.end ())
57  {
58  retval = *p;
59  m_handle_free_list.erase (p);
60  }
61  else
62  {
63  retval = url_handle (m_next_handle);
64 
66  }
67 
68  return retval;
69 }
70 
72 {
73  if (h.ok ())
74  {
75  auto p = m_handle_map.find (h);
76 
77  if (p == m_handle_map.end ())
78  error ("url_handle_manager::free: invalid object %g", h.value ());
79 
80  // Curl handles are negative integers plus some random
81  // fractional part. To avoid running out of integers, we
82  // recycle the integer part but tack on a new random part
83  // each time.
84 
85  m_handle_map.erase (p);
86 
87  if (h.value () < 0)
88  m_handle_free_list.insert
89  (std::ceil (h.value ()) - make_handle_fraction ());
90  }
91 }
92 
OCTAVE_END_NAMESPACE(octave)
double value(void) const
Definition: oct-handle.h:78
bool ok(void) const
Definition: oct-handle.h:113
Definition: oct-rand.h:45
std::map< url_handle, url_transfer > m_handle_map
void free(const url_handle &h)
url_handle get_handle(void)
std::set< url_handle > m_handle_free_list
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition: error.cc:979
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:103
static double make_handle_fraction(void)
octave_handle url_handle