GNU Octave  9.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-2024 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
39 make_handle_fraction ()
40 {
41  static double maxrand = RAND_MAX + 2.0;
42 
43  return (rand () + 1.0) / maxrand;
44 }
45 
48 {
49  url_handle retval;
50 
51  // Curl handles are negative integers plus some random fractional
52  // part. To avoid running out of integers, we recycle the integer
53  // part but tack on a new random part each time.
54 
55  auto p = m_handle_free_list.begin ();
56 
57  if (p != m_handle_free_list.end ())
58  {
59  retval = *p;
60  m_handle_free_list.erase (p);
61  }
62  else
63  {
64  retval = url_handle (m_next_handle);
65 
66  m_next_handle = std::ceil (m_next_handle) - 1.0 - make_handle_fraction ();
67  }
68 
69  return retval;
70 }
71 
72 void
74 {
75  if (h.ok ())
76  {
77  auto p = m_handle_map.find (h);
78 
79  if (p == m_handle_map.end ())
80  error ("url_handle_manager::free: invalid object %g", h.value ());
81 
82  // Curl handles are negative integers plus some random
83  // fractional part. To avoid running out of integers, we
84  // recycle the integer part but tack on a new random part
85  // each time.
86 
87  m_handle_map.erase (p);
88 
89  if (h.value () < 0)
90  m_handle_free_list.insert
91  (std::ceil (h.value ()) - make_handle_fraction ());
92  }
93 }
94 
95 OCTAVE_END_NAMESPACE(octave)
double value() const
Definition: oct-handle.h:78
bool ok() const
Definition: oct-handle.h:113
Definition: oct-rand.h:45
void free(const url_handle &h)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
Definition: error.cc:988
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:103
octave_handle url_handle